1

我第一次尝试创建自定义渲染器。我要做的就是更改 ListView 中 TextCell 的字体大小。

我已经在此处查看指南http://developer.xamarin.com/guides/cross-platform/xamarin-forms/custom-renderer/以获取条目单元格,但不知道如何处理 TextCell 并且找不到任何地方的信息(它可能非常基本,但我对 Xamarin 很陌生)

Entry 单元格的代码如下(适用于 Android)

public class MyEntryRenderer : EntryRenderer
{
  // Override the OnElementChanged method so we can tweak this renderer post-initial setup
  protected override void OnElementChanged (ElementChangedEventArgs<Entry> e)
  {
    base.OnElementChanged (e));
    if (e.OldElement == null) {   // perform initial setup
      // lets get a reference to the native control
      var nativeEditText = (global::Android.Widget.EditText) Control;
      // do whatever you want to the textField here!
      nativeEditText.SetBackgroundColor(global::Android.Graphics.Color.DarkGray);
    }
  }
}

那么在 TextCell 的情况下,我要覆盖什么?(如果我使用 OnElementChanged,它不会给我 OnElementChanged 作为基础 - 它确实给了我 OnCellPropertyChanged 但如果我将它用于方法,那么它似乎想要 PropertyChangedEventArgs 然后它不喜欢它 --- 我没有知道该怎么做,这让我发疯

任何建议表示赞赏

4

1 回答 1

4

不确定这是否是您要查找的内容,但这应该可以。您可以操作文本视图以及.TextCell

我认为您最好使用 aViewCell虽然,因为您可以更好地控制包含的内容及其呈现方式。

class MyTextCellRenderer : TextCellRenderer
{
    protected override View GetCellCore(Cell item, View convertView, ViewGroup parent, Context context)
    {
        var cell = (LinearLayout) base.GetCellCore(item, convertView, parent, context);
        var textView = (TextView)(cell.GetChildAt(1) as LinearLayout).GetChildAt(0);
        var detailView = (TextView)(cell.GetChildAt(1) as LinearLayout).GetChildAt(1);
        textView.TextSize = textView.DipsToPixels(32);
        return cell;
    }
}

public static class LayoutHelperExtensions
{
    public static int DipsToPixels(this View view, float dip)
    {
        return (int) Math.Round(TypedValue.ApplyDimension(ComplexUnitType.Dip, dip, view.Resources.DisplayMetrics));
    }
}
于 2014-09-12T17:33:15.620 回答