Android:图层列表无法工作?

9

我正在尝试将两个椭圆形可绘制对象叠放在一起,第一个具有透明度。但是,按照我的方式,它会以第二个椭圆的大小显示第一个椭圆的颜色。

<?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>

我该如何让它按照预期工作?
编辑:这是父级元素:
<?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>

您的XML代码已经正确设置。问题可能在于“parent”视图托管了层列表。您能否发布包含层列表的代码? - Fuzzical Logic
我发布了我正在使用的ImageView。我也尝试将其设置为按钮背景。 - Flynn
你尝试过为ImageView使用显式宽度吗?例如15-20 dp。 - Fuzzical Logic
另外,你尝试过将Drawables分开并独立绘制以确保它们能够独立工作吗?请注意,你本不应该这样做,但这只是调试步骤。 - Fuzzical Logic
1个回答

8
经过对不同类型XML可绘制对象的研究,看来您的LayerDrawable(layer-list)正在独立缩放ShapeDrawables。然后,ImageView又在缩放LayerDrawable。根据谷歌的这篇指南,同时缩放ShapeDrawableLayerDrawable都是一个问题。 LayerDrawable将检查您拥有的每个项目所需的缩放。他们有几种解决方案:
  • gravity设置为不进行缩放的内容,例如“center”。
  • 将可绘制对象定义为位图。
  • 将ImageView设置为不进行缩放的scaleType。
然而,这里有两个主要问题...您只能使用bitmapgravity。并且您无法在XML中使用ShapeDrawable作为bitmap。我尝试了所有我能想到的方法来解决它。这是唯一为我解决问题的方法。
<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>

所以:我删除了LayerDrawable,将形状分离成它们自己的XML,并创建了两个ImageView。(为了方便,我将它们放在FrameLayout中)。这是停止缩放的唯一方法。

测试步骤

已在Android 2.1、3.0、4.0中进行测试

  • 更改图像scaleType
  • 更改图像width和height
  • 分离ShapeDrawables
  • 将LayerList更改为仅具有引用分离的shape的drawable属性的项目
  • 将LayerList更改为引用分离的shape的位图。
  • 更改shape的顺序

另外,你也可以在代码中实现。



谢谢!建议在ImageView中测试scaleType,解决了我的层列表绘制问题。 - bogumil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接