Xamarin:Tableview rowheight不适用于android

时间:2018-02-09 12:22:20

标签: xamarin xamarin.forms xamarin.android

我正在使用Xamarin.Forms应用,其中TableView只有自定义ViewCell

在PCL:

public class MyViewCell : ViewCell
{
   public static readonly BindableProperty NoTapProperty =
          BindableProperty.Create("NoTap", typeof(bool), typeof(CheckedTextCell), defaultValue: false);

   public bool NoTap
   {
      get { return (bool)GetValue(NoTapProperty); }
      set { SetValue(NoTapProperty, value); }
   }
}

在iOS中:

public class MyViewCellRenderer : ViewCellRenderer
{
   UITableViewCell _nativeCell;

   public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
   {
      _nativeCell = base.GetCell(item, reusableCell, tv);
      var formsCell = item as MyViewCell;

      if (formsCell != null)
      {
          formsCell.PropertyChanged -= OnPropertyChanged;
          formsCell.PropertyChanged += OnPropertyChanged;
       }
       SetTap(formsCell);
       return _nativeCell;
    }

    void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
       var formsCell = sender as MyViewCell;
       if (formsCell == null) return;
       if (e.PropertyName == MyViewCell.NoTapProperty.PropertyName)
       {
          SetTap(formsCell);
        }
     }

     void SetTap(MyViewCell formsCell)
     {
        if (formsCell.NoTap) _nativeCell.SelectionStyle = UITableViewCellSelectionStyle.None;
        else _nativeCell.SelectionStyle = UITableViewCellSelectionStyle.Default;
      }
}

在Android中:

public class MyViewCellRenderer : ViewCellRenderer
{
   Android.Views.View _nativeCell;

   protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, Android.Views.ViewGroup parent, Android.Content.Context context)
   {
       _nativeCell = base.GetCellCore(item, convertView, parent, context);

       var formsCell = item as MyViewCell;

       if (formsCell != null)
       {
          formsCell.PropertyChanged -= OnPropertyChanged;
          formsCell.PropertyChanged += OnPropertyChanged;
       }

       SetTap(formsCell);
       return _nativeCell;
   }

   void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
   {
      var formsCell = sender as MyViewCell;
      if (formsCell == null) return;

      if (e.PropertyName == MyViewCell.NoTapProperty.PropertyName)
      { SetTap(formsCell); }
   }

   void SetTap(MyViewCell formsCell)
   {
      if (formsCell == null) return;
      _nativeCell.Clickable = formsCell.NoTap;
    }
}

然后我在XAML中使用它,如下所示:

<TableView Intent="Settings" RowHeight="50" HasUnevenRows="true">
   <TableView.Root>
      <TableSection>
         <local:MyViewCell NoTap="true" Height="70">
            <!-- More codes here -->
         </local:MyViewCell>
      </TableSection>
      <TableSection Title="Study Mode">
         <local:MyViewCell NoTap="true">
            <Grid VerticalOptions="CenterAndExpand" Padding="20, 0">
               <Label Style="{DynamicResource ListItemDetailTextStyle}" YAlign="Center" Text="Phrase Display" HorizontalOptions="StartAndExpand" />
               <!-- More code for segmented control -->
            </Grid>
         </local:MyViewCell>
         <local:MyViewCell x:Name="btn" NoTap="true">
            <Grid VerticalOptions="CenterAndExpand" Padding="20, 0">
               <!-- More codes here -->
             </Grid>
         </local:MyViewCell>
      </TableSection>
   </TableView.Root>
</TableView>

RowHeight="50"完全适用于iOS(意味着所有表行具有相同的高度)。不幸的是,在Android中并非如此。请参阅下面的图片。

机器人:

enter image description here

的iOS: enter image description here

任何人都知道我错过了什么?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用Padding来控制元素的渲染位置。在Grid视图中,将Padding"20, 0"修改为"20, 10, 0, 10"

这是我的代码:

<TableView Intent="Settings" RowHeight="50" HasUnevenRows="true">
    <TableView.Root>
        <TableSection >
            <local:MyViewCell NoTap="true" Height="70" >
                <!-- More codes here -->
                <Label  YAlign="Center" Text="Phrase Display" HorizontalOptions="StartAndExpand"/>
            </local:MyViewCell>
        </TableSection>
        <TableSection Title="Study Mode">
            <local:MyViewCell NoTap="true">
                <Grid VerticalOptions="CenterAndExpand" Padding="20, 10, 0, 10">
                    <Label  YAlign="Center" Text="Phrase Display" HorizontalOptions="StartAndExpand"/>
                    <!-- More code for segmented control -->
                </Grid>
            </local:MyViewCell>
            <local:MyViewCell x:Name="btn" NoTap="true">
                <Grid VerticalOptions="CenterAndExpand" Padding="20, 10, 0, 10">
                    <!-- More codes here -->
                     <StackLayout>
                        <Label  YAlign="Center" Text="Phrase Display" HorizontalOptions="StartAndExpand" />
                        <Label  YAlign="Center" Text="Phrase Display" HorizontalOptions="StartAndExpand" />
                        <Label  YAlign="Center" Text="Phrase Display" HorizontalOptions="StartAndExpand" />
                    </StackLayout>
                 </Grid>
            </local:MyViewCell>
        </TableSection>
    </TableView.Root>
</TableView>
使用"20, 0"作为Grid Padding

Effect

使用"20, 10, 0, 10"作为Grid Padding

Effect