Android:在可绘制的xml中,在图层列表中,为什么项目的宽度在API 19或更低版本中不起作用? [英] Android: in drawable xml, layer-list, why does the width for item not work in API 19 or lower?

查看:120
本文介绍了Android:在可绘制的xml中,在图层列表中,为什么项目的宽度在API 19或更低版本中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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中不起作用,宽度被忽略,矩形将填充整个视图.

It works in Lollipop or higher, but it doesn't work in Kitkat, the width is being ignored, the rectangle will fill the whole view.

我还试图删除第二个项目,只保留了第一个项目,宽度仍然被忽略.

I also tried to remove the second item, only leaves the first item, the width is still being ignored.

ImageView使用此可绘制对象:

The ImageView uses this 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"
    />

如何解决这个问题?

推荐答案

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

Alright, I'll answer my own question:

似乎在Android 4.4或更低版本上,大小节点或width/height属性将被忽略.
因此最好的方法是:在不同的可绘制xml文件中创建不同的形状,创建相对布局而不是ImageView,然后将它们组合在相对布局中.

It seems that on Android 4.4 or below, size node or width/height attribute will be ignored.
So the best approach is: create different shapes in different drawable xml files, create a relative layout instead of ImageView, and combine them in relative layout.

<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:在可绘制的xml中,在图层列表中,为什么项目的宽度在API 19或更低版本中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆