【问题标题】:How to change the TableSection text color with a custom renderer - Xamarin.Forms C#如何使用自定义渲染器更改 TableSection 文本颜色 - Xamarin.Forms C#
【发布时间】:2016-09-09 06:00:52
【问题描述】:

我有一个 TableView,根中有 3 个 TableSections:

var tableView = new TableView
{
  RowHeight = 60,
  Root = new TableRoot
  {
    firstTableSection,
    secondTableSection,
    thirdTableSection,

  }
}

var firstTableSection = new TableSection("First")
{
  // Cells
}

var firstTableSection = new TableSection("First")
{
  // Cells
}

var firstTableSection = new TableSection("First")
{
  // Cells
}

如何使用自定义渲染器更改 TableSection 文本的文本颜色?

public class TestTableViewRenderer : Xamarin.Forms.Platform.Android.TableViewRenderer
{
    protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Xamarin.Forms.TableView> e)
    {
        base.OnElementChanged(e);

        // Not sure how to add this property here

    }
}

【问题讨论】:

    标签: c# android xamarin xamarin.forms custom-renderer


    【解决方案1】:

    我最终使用了这个解决方案:

    https://forums.xamarin.com/discussion/32379/changing-the-title-color-of-a-tablesection

    这依赖于两个自定义渲染器:

    [assembly: ExportRenderer(typeof(TableView), typeof(CustomTableView))]
    namespace APP.Droid
    {
        public class CustomTableView : TableViewRenderer
        {
            protected override TableViewModelRenderer GetModelRenderer(global::Android.Widget.ListView listView, TableView view)
            {
                return new CustomTableViewModelRenderer(this.Context, listView, view);
            }
    
        }
    }
    

    [assembly: ExportRenderer(typeof(TableViewModelRenderer), typeof(CustomTableViewModelRenderer))]
    namespace APP.Droid
    {
        public class CustomTableViewModelRenderer : TableViewModelRenderer
        {
            public CustomTableViewModelRenderer(Context Context, global::Android.Widget.ListView ListView, TableView View)
                : base(Context, ListView, View)
            { }
            public override global::Android.Views.View GetView(int position, global::Android.Views.View convertView, ViewGroup parent)
            {
                var view = base.GetView(position, convertView, parent);
    
                var element = this.GetCellForPosition(position);
    
                if (element.GetType() == typeof(TextCell))
                {
                    try
                    {
                        var text = ((((view as LinearLayout).GetChildAt(0) as LinearLayout).GetChildAt(1) as LinearLayout).GetChildAt(0) as TextView);
                        var divider = (view as LinearLayout).GetChildAt(1);
    
                        text.SetTextColor(Android.Graphics.Color.Rgb(50, 50, 50));
                        divider.SetBackgroundColor(Android.Graphics.Color.Rgb(150, 150, 150));
                    }
                    catch (Exception) { }
                }
    
                return view;
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-24
        • 2021-10-05
        • 1970-01-01
        • 1970-01-01
        • 2020-10-19
        • 1970-01-01
        • 2014-08-29
        • 2016-05-31
        相关资源
        最近更新 更多