Android:Layer-List无效?

Fly*_*ynn 7 android android-layout android-xml

我试图把两个椭圆形的抽屉放在一起,第一个有透明度.然而,按照我的方式,它以第二个椭圆的大小显示第一个椭圆的颜色.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
    <shape android:shape="oval">
        <size android:width="15dp" android:height="15dp" />
        <solid android:color="#35000000"/>
    </shape>
</item>
<item>
    <shape android:shape="oval">
        <size android:width="5dp" android:height="5dp" />
        <solid android:color="#000000"/>
    </shape>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

我怎样才能让它按预期工作?

编辑: 这是父母:

<?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="match_parent"
android:orientation="vertical" >
<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/my_layerlist" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Fuz*_*gic 8

在对不同类型的XML drawable进行大量研究之后,您的LayerDrawable(layer-list)似乎是ShapeDrawables独立扩展的.然后,ImageView正在缩放LayerDrawable.据来自谷歌本指南,用于缩放两个 ShapeDrawable S和LayerDrawables是一个问题.LayerDrawables将检查您拥有的每个项目的必要缩放比例.他们有几个解决方案:

  • 设置为gravity不缩放的内容,例如"center".
  • 将drawable定义为位图.
  • 将ImageView设置为不缩放的scaleType.

这有两个主要问题,但是......你只能gravity用于bitmap.而且你不能bitmap在XML中使用ShapeDrawable .我尝试了一切我能想到的事情来做对.这是唯一能解决它的问题.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/oval1"
    android:scaleType="center"  />
<ImageView
    android:id="@+id/imageView2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/oval2"
    android:scaleType="center"  />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

所以:我删除了LayerDrawable,将Shapes分成了自己的XML,制作了两个ImageView.(为了方便我将它们卡在FrameLayout中).这是阻止缩放的唯一方法.


测试程序

在Android 2.1,3.0,4.0中测试过

  • 改变了形象 scaleType
  • 改变了形象widthheight
  • 分离的ShapeDrawables
  • 将LayerList更改为仅item使用drawable属性引用分隔的shapes
  • 将LayerList更改为bitmaps引用分隔的shapes.
  • 改变了shapes的顺序

或者,您可以在代码中执行此操作.