【问题标题】:Content Inset with Custom Drawable Scaling具有自定义可绘制缩放的内容插图
【发布时间】:2017-01-01 12:29:42
【问题描述】:

我有一个 ListView,其中包含在 relativeLayout 中定义的自定义项目,其中包括一个带有 android:layout_width="match_parent"android:layout_height="wrap_content" 的 imageView 和一个自定义矢量可绘制作为源,其示意图如下所示:

+-------------------------------------------+
+    solid area              | some picture +
+-------------------------------------------+

问题是当我切换到横向模式时,图像会放大(以匹配父级的宽度),但是保持纵横比也会增加高度。

理想情况下,我想定义一个 Content Inset(我从 iOS 知道它),这样上面的实心区域就会被拉伸以匹配新的宽度,并且“一些图片”区域保持相同的纵横比.总而言之,我会进行缩放以使图像的高度(以及整个 listView 项的高度)保持不变。

【问题讨论】:

  • 尝试 ShapeDrawable 将自定义 Shape 对象传递给 ShapeDrawable 构造函数
  • @pskink 感谢您的回复——不确定该走哪条路?能具体一点吗?
  • 如果你想要自定义缩放,你需要在自定义 Shape#draw 方法中绘制你的东西

标签: android android-layout android-imageview listviewitem android-relativelayout


【解决方案1】:

您可以通过使用LinearLayout 在子宽度中设置权重 来拉伸实心区域。要完成这项工作,您应该为ImageView 分配一个固定宽度。

此外,ImageView 上的 adjustViewBounds 属性将有助于保持正确的纵横比。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:orientation="horizontal">

    <!-- this can be any view or view group, the layout params are the important part-->
    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        ...
        />

    <!-- set this to an absolute width that will be the same for portrait & landscape -->
    <ImageView
        android:layout_width="240dp"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        ...
        />

</LinearLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-23
    • 2012-11-16
    • 2013-02-07
    • 2011-02-06
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 2013-01-06
    相关资源
    最近更新 更多