简体   繁体   English

Android:Layer-List无效?

[英]Android: Layer-List not working?

I am trying to put two oval drawables on top of each other, the first has transparency. 我试图把两个椭圆形的抽屉放在一起,第一个有透明度。 However with the way I have it, it displays the first oval's color at the size of the second oval. 然而,按照我的方式,它以第二个椭圆的大小显示第一个椭圆的颜色。

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

How can I get this to work as intended? 我怎样才能让它按预期工作?

EDIT: Here is the parent: 编辑:这是父母:

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

After doing much research on the different types of XML drawables, it appears that your LayerDrawable (layer-list) is be scaling the ShapeDrawables independently. 在对不同类型的XML drawable进行了大量研究之后,看起来你的LayerDrawable (layer-list)正在独立地扩展ShapeDrawables Then , the ImageView is scaling the LayerDrawable . 然后ImageView正在缩放LayerDrawable According to this guide from Google , scaling for both ShapeDrawable s and LayerDrawable s is a concern. 来自谷歌本指南 ,缩放两个 ShapeDrawable S和LayerDrawable s是一个问题。 LayerDrawable s will check for necessary scaling on each item you have. LayerDrawable将检查您拥有的每个项目的必要缩放。 They have several solutions: 他们有几个解决方案:

  • Set the gravity to something that does not scale, such as "center". gravity设置为不缩放的东西,例如“中心”。
  • define the drawable as a bitmap. 将drawable定义为位图。
  • Set the ImageView to scaleType that does not scale. 将ImageView设置为不缩放的scaleType。

There is two major problem with this, however... You can only use gravity for the bitmap . 这有两个主要问题,但是......你只能使用gravity作为bitmap And you cannot use a ShapeDrawable for the bitmap in XML. 并且您不能将ShapeDrawable用于XML中的bitmap I tried everything I could think of to get it right. 我尝试了一切我能想到的事情来做对。 Here's the only thing that fixed it for me. 这是唯一能解决它的问题。

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

So: I removed the LayerDrawable, separated the Shapes into their own XMLs, made two ImageViews. 所以:我删除了LayerDrawable,将Shapes分成了自己的XML,制作了两个ImageView。 (for ease I stuck them in a FrameLayout). (为了方便我将它们卡在FrameLayout中)。 This was the only way to stop the scaling. 这是阻止缩放的唯一方法。


Testing Procedure 测试程序

Tested in Android 2.1, 3.0, 4.0 在Android 2.1,3.0,4.0中测试过

  • Changed Image scaleType 更改了图像scaleType
  • Changed Image width and height 更改图像widthheight
  • Separated ShapeDrawables 分离的ShapeDrawables
  • Changed LayerList to just item s with drawable attribute referencing separated shape s 将LayerList更改为仅具有drawable属性的item s,引用分离的shape s
  • Changed LayerList to bitmap s referencing separated shape s. 将LayerList更改为引用分离的shapebitmap
  • Changed order of shape s 改变了shape的顺序

Alternatively, you could do it in code. 或者,您可以在代码中执行此操作。


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM