【问题标题】:TableView section header in android is blank and clickableandroid中的TableView部分标题为空白且可点击
【发布时间】:2014-08-17 22:06:27
【问题描述】:

我正在使用Xamarin.Forms,目前正在尝试制作没有节标题的TableView。现在在 iOS 上看起来不错,因为部分标题不可见或不可点击,但在 Android 上,标题是空白、可见且可点击的。

我试过这个http://forums.xamarin.com/discussion/18037/tablesection-w-out-header

xaml 中的代码 -

<TableView>
    <TableView.Root>
      <TableSection>
        <TextCell Text="Login" Command="{Binding Login}" />
        <TextCell Text="Sign Up" Command="{Binding SignUp}" />
        <TextCell Text="About" Command="{Binding About}"/>
      </TableSection>
    </TableView.Root>
  </TableView>

c#代码

Content = new TableView
{
    Root = new TableRoot 
    {
        new TableSection () 
        {
            new TextCell { Text="Login", Command = Login }, 
            new TextCell { Text="Sign Up", Command = SignUp },
            new TextCell { Text="About", Command = About },
        },
    },
};

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    为了抑制 Android 上的标头,我们使用自定义渲染器。如果Text 为空,它会通过移除所有子项、降低高度并移除所有填充来隐藏单元格。

    [assembly: ExportRenderer(typeof(TextCell), typeof(ImprovedTextCellRenderer))]
    
    namespace YourSolution.Android
    {
        public class ImprovedTextCellRenderer : TextCellRenderer
        {
            protected override global::Android.Views.View GetCellCore(Cell item, global::Android.Views.View convertView, ViewGroup parent, Context context)
            {
                var view = base.GetCellCore(item, convertView, parent, context) as ViewGroup;
                if (String.IsNullOrEmpty((item as TextCell).Text)) {
                    view.Visibility = ViewStates.Gone;
                    while (view.ChildCount > 0)
                        view.RemoveViewAt(0);
                    view.SetMinimumHeight(0);
                    view.SetPadding(0, 0, 0, 0);
                }
                return view;
            }
        }
    }
    

    只需将这个类复制到您的 Android 项目中的某个位置,就可以了。

    【讨论】:

    • 工作得很好,谢谢 Falko
    • @Falko,你的 TableView 有多少行?当我将此解决方案应用于我的 TableView 时,一些尚不可见的行的内容为空。
    • 我们如何为winPhone创建渲染器?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多