Android:在 drawable xml、layer-list 中,为什么项目的宽度在 API 19 或更低版本中不起作用?

标签 android xml-drawable

<layer-list
xmlns:android="http://schemas.android.com/apk/res/android"
>
<!--width here doesn't work-->
<item
    android:gravity="left"
    android:width="10dp"
    >
    <shape
        android:shape="rectangle"
    >
        <stroke
            android:width="1dp"
            android:color="@color/black"
        />
        <!--<gradient-->
            <!--android:startColor="#00dddddd"-->
            <!--android:endColor="#40404040"-->
            <!--android:angle="0"-->
            <!--/>-->
    </shape>
</item>
<!--width here doesn't work-->
<item
    android:width="10dp"
    android:gravity="right"
    >
    <shape
        android:shape="rectangle"
    >
        <stroke
            android:width="1dp"
            android:color="@color/black"
        />
        <!--<gradient-->
            <!--android:startColor="#00dddddd"-->
            <!--android:endColor="#40404040"-->
            <!--android:angle="180"-->
            <!--/>-->
    </shape>
</item>

这是例子。

它在 Lollipop 或更高版本中有效,但在 Kitkat 中无效,宽度被忽略,矩形将填满整个 View 。

我也试过删除第二个项目,只留下第一个项目,宽度仍然被忽略。

ImageView 使用这个 drawable:

<!--Width will be changed dynamically-->
<ImageView
    android:id="@+id/someid"
    android:layout_width="100dp"
    android:layout_height="@dimen/someheight"
    android:src="@drawable/the_xml_above"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:clickable="false"
    />

如何解决?

最佳答案

好吧,我会回答我自己的问题:

似乎在 Android 4.4 或更低版本上,尺寸节点或宽度/高度属性将被忽略。
所以最好的办法是:在不同的drawable xml文件中创建不同的形状,创建一个相对布局而不是ImageView,然后将它们组合在相对布局中。

<RelativeLayout
    android:id="@+id/id1"
    android:layout_width="30dp"
    android:layout_height="@dimen/someheight"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:clickable="false">

    <ImageView
        android:layout_width="3dp"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:src="@drawable/somesrc"/>

    <ImageView
        android:layout_width="3dp"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:rotation="180"
        android:src="@drawable/somesrc"/>

</RelativeLayout>

关于Android:在 drawable xml、layer-list 中,为什么项目的宽度在 API 19 或更低版本中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36743575/

相关文章:

java - 比较器 'comparing' 无法解析方法

android - 如何动态设置弹出窗口线性布局的背景?

android - setBackgroundDrawable 无法正常工作

Android Unresolved reference : findNavController error

android - 如何在 Android Studio 中使用 Facebook Buck 构建工具?

android - ImageView 中未显示相机文件夹图像

android - AsyncTask,Insert,在 Android 中选择错误的执行顺序

android - 使用自定义可绘制 xml 的涟漪效应?

当我尝试创建可绘制资源文件时,Android Studio 出现奇怪的错误