【问题标题】:Xamarin Forms Resize Image inside ListviewXamarin 表单在 Listview 中调整图像大小
【发布时间】:2014-10-26 22:31:21
【问题描述】:

如何在列表视图中设置图像的大小。目前我有几个带有图标的列表,但我没有看到任何用于更改图像纵横比或大小的选项,因此它只会被放大到列表项的高度。

所有图片均来自 Drawable 文件夹

        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;
        }

【问题讨论】:

    标签: xamarin android-imageview android-image xamarin.forms


    【解决方案1】:

    您现在正在处理一个 Android ImageView

    你有你的参考通过var icon = (ImageView)cell.GetChildAt(0)

    您现在可以通过 .setScaleType() 等方法自定义它以获得相关的纵横比,并使用 .layout() 更改位置/大小。

    【讨论】:

    • so .Layout() 似乎可以很好地处理位置,但我没有看到任何与大小相关的内容。
    • @BillPull - 调整宽度和高度的第三个和第四个参数。
    • 这可以在 XAML 中声明吗?
    • @ScottNimrod 这是Android 的特定平台。我用您的 ListView 回答了您关于 XAML 中图像大小的其他问题,您最有可能对它更感兴趣? stackoverflow.com/questions/35647976/…)
    【解决方案2】:

    在您的 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>
    

    【讨论】:

      猜你喜欢
      • 2018-10-20
      • 1970-01-01
      • 1970-01-01
      • 2018-09-27
      • 2021-10-12
      • 1970-01-01
      • 2017-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多