图层列表可绘制未正确绘制

And*_*ord 2 xml user-interface android drawable

我正在尝试为我的 Android 应用程序创建一个可绘制的简单图层列表。当我将 drawable 设置为 ImageView 的 src 时,它没有正确绘制:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:top="20dp">
        <shape android:shape="oval">
            <solid android:color="@color/dark_gray" />
        </shape>
    </item>

    <item android:left="20dp">
        <shape android:shape="oval">
            <solid android:color="@color/dark_gray" />
        </shape>
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

只是两个椭圆形,彼此有点偏移。没什么特别的(只是一个测试),但它不起作用。

看来这android:left|right|bottom|top就是问题所在。如果我只使用这些命令中的一个,drawable 就会被正确绘制。如果使用两个或更多,则 ImageView 保持为空。

作品:

   <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
      <item android:top="20dp">
         <shape android:shape="oval">
            <solid android:color="@color/dark_gray" />
         </shape>
      </item>

      <item>
         <shape android:shape="oval">
            <solid android:color="@color/dark_gray" />
         </shape>
      </item>
   </layer-list>
Run Code Online (Sandbox Code Playgroud)

不起作用(如第一个示例):

   <?xml version="1.0" encoding="utf-8"?>
   <layer-list xmlns:android="..." >
      <item android:top="20dp" android:left="20dp">
         <shape android:shape="oval">
            <solid android:color="@color/dark_gray" />
         </shape>
      </item>
   </layer-list>
Run Code Online (Sandbox Code Playgroud)

这里有什么问题?

编辑: @ Rod_Algonquin 我使用一个非常简单的布局来测试可绘制对象:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:background="#aaa" >

    <ImageView
        android:id="@+id/image_logo"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/testDrawable" >
    </ImageView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

似乎android:left|right|bottom|top参数的数量不是问题的根源,而是使用的具体值。

这个 drawable 工作正常,并且:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:top="2dp">
        <shape android:shape="oval">
            <solid android:color="#0f0" />  
        </shape>
    </item> 
</layer-list>
Run Code Online (Sandbox Code Playgroud)

这个没有绘制任何东西(ImageView 只是透明的):

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:top="1dp">
        <shape android:shape="oval">
            <solid android:color="#0f0" />  
        </shape>
    </item> 
</layer-list>
Run Code Online (Sandbox Code Playgroud)

这里唯一的区别是 的值android:top。1dp 不起作用。2dp 工作没有任何问题。

任何的想法?

Rod*_*uin 5

问题是您使用layer-listdrawable 作为 ImageView 的图像资源。android:src只会接受图像而不是可绘制的图像,因此在那里什么也没有。

解决方案:

而不是把它android:src放在把它放在background你可以自由使用 drawable 的地方。

 <ImageView
    android:id="@+id/image_logo"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="@drawable/testDrawable" >
</ImageView>
Run Code Online (Sandbox Code Playgroud)