Xamarin在列表视图中调整图像大小Image
问题描述:
如何在列表视图中设置图像的大小。目前,我有几个有图标的列表,但我没有看到任何更改图像宽高比或大小的选项,因此它只会被炸到列表项的高度。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;
}
答
你现在正在处理的的AndroidImageView的。
你有参考通过var icon = (ImageView)cell.GetChildAt(0)
。
现在可以通过东西自定义为相关的纵横比,如.SetScaleType,并使用.Layout()改变位置/大小。
答
在你的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>
所以.Layout()好像它与位置的微交易,但我没有看到相关的大小事情。 – BillPull 2014-10-26 23:19:05
@BillPull - 它的第三个和第四个参数来调整宽度和高度。 – Pete 2014-10-26 23:51:30
这可以在XAML中声明吗? – 2016-02-26 09:05:57