2014-10-26 308 views
0

如何在列表視圖中設置圖像的大小。目前,我有幾個有圖標的列表,但我沒有看到任何更改圖像寬高比或大小的選項,因此它只會被炸到列表項的高度。Xamarin在列表視圖中調整圖像大小Image

所有圖片均來自可繪製文件夾

 var cell = new DataTemplate(typeof(MenuTextCell)); 
     cell.SetBinding(TextCell.TextProperty, "Title"); 
     cell.SetBinding(ImageCell.ImageSourceProperty, "IconSource"); 
     cell.SetValue(BackgroundColorProperty, Color.Transparent); 
     cell.SetValue(TextCell.TextColorProperty, Color.FromHex("262626")); 

我使用的是自定義渲染

public class MenuTextCellRenderer : ImageCellRenderer 
    { 
     protected override View GetCellCore (Cell item, View convertView, ViewGroup parent, Context context) 
     { 
      var cell = (LinearLayout)base.GetCellCore (item, convertView, parent, context); 
      cell.SetPadding(20, 10, 0, 10); 
      cell.DividerPadding = 50; 

      var div = new ShapeDrawable(); 
      div.SetIntrinsicHeight(1); 
      div.Paint.Set(new Paint { Color = Color.FromHex("b7b7b7").ToAndroid() }); 

      if (parent is ListView) 
      { 
       ((ListView)parent).Divider = div; 
       ((ListView)parent).DividerHeight = 1; 
      } 

      var icon = (ImageView)cell.GetChildAt(0); 

      var label = (TextView)((LinearLayout)cell.GetChildAt(1)).GetChildAt(0); 
      label.SetTextColor(Color.FromHex("262626").ToAndroid()); 
      label.TextSize = Font.SystemFontOfSize(NamedSize.Large).ToScaledPixel(); 
      label.TextAlignment = TextAlignment.Center; 
      label.Text = label.Text.ToUpper(); 

      var secondaryLabel = (TextView)((LinearLayout)cell.GetChildAt(1)).GetChildAt(1); 
      secondaryLabel.SetTextColor(Color.FromHex("262626").ToAndroid()); 
      secondaryLabel.TextSize = Font.SystemFontOfSize(NamedSize.Large).ToScaledPixel(); 
      label.TextAlignment = TextAlignment.Center; 


      return cell; 
     } 

回答

1

你現在正在處理的的AndroidImageView的

你有參考通過var icon = (ImageView)cell.GetChildAt(0)

現在可以通過東西自定義爲相關的縱橫比,如.SetScaleType,並使用.Layout()改變位置/大小。

+0

所以.Layout()好像它與位置的微交易,但我沒有看到相關的大小事情。 – BillPull 2014-10-26 23:19:05

+0

@BillPull - 它的第三個和第四個參數來調整寬度和高度。 – Pete 2014-10-26 23:51:30

+0

這可以在XAML中聲明嗎? – 2016-02-26 09:05:57

0

在你的ListView.ItemTemplate上試試類似的東西,而不是使用imagecell。您也可以編寫自定義單元格。

<DataTemplate> 
<ViewCell> 
    <StackLayout VerticalOptions="FillAndExpand" Orientation="Horizontal" Padding="10"> 
     <Image Aspect="AspectFill" HeightRequest ="20" WidthRequest="20" Source="{Binding IconSource}" /> 
     <Label Text="{Binding Title}" YAlign="Center" Font="Medium" /> 
    </StackLayout> 
</ViewCell>