7

我正在使用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 },
        },
    },
};
4

1 回答 1

7

为了抑制 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 项目中的某个位置,就可以了。

于 2014-08-04T21:32:08.387 回答