简体   繁体   English

Android XML图层列表:如何定位顶层

[英]Android XML layer-list: How to position the top layer

I have this XML drawable - tab_background_unselected: 我有这个XML drawable - tab_background_unselected:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@color/background_grey" />            
        </shape>
    </item>

</layer-list>

which creates this shape: 创建这个形状:

背景矩形

and this arrow shape xml drawable - tab_selected_arrow: 这个箭头形状xml drawable - tab_selected_arrow:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <rotate
            android:fromDegrees="45"
            android:pivotX="-40%"
            android:pivotY="87%"
            android:toDegrees="45" >
            <shape android:shape="rectangle" >
                <solid android:color="@color/background_dark_green" />
            </shape>
        </rotate>
    </item>

</layer-list>

which creates this shape: 创建这个形状:

顶层三角形

I'm using this drawable XML (instead of PNG file) in order to create a layer-list: 我正在使用这个可绘制的XML(而不是PNG文件)来创建一个图层列表:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@drawable/tab_background_unselected">
    </item>
    <item android:drawable="@drawable/tab_selected_arrow">
    </item>

</layer-list>

but I want the final image to look like this: 但我希望最终图像看起来像这样:

最终形象

I don't know how to set the gravity of the arrow (the second item and the top layer) to center|bottom... I've tried using bitmap tag but it only accepts image files. 我不知道如何将箭头的重力(第二项和顶层)设置为居中|我已经尝试使用bitmap标签,但它只接受图像文件。

I need this to be a XML drawable because 我需要这是一个XML drawable因为

  1. I need it to be inside a drawable selector 我需要它在一个可绘制的选择器内
  2. I don't want to make it a PNG and create different file for each screen resolution 我不想让它成为PNG并为每个屏幕分辨率创建不同的文件

Use an inner Bitmap drawable inside the item , and set gravity for it: item使用内部Bitmap drawable,并为其设置gravity

<item android:drawable="@drawable/tab_background_unselected">
</item>
<item>
    <bitmap
        android:gravity="bottom|center_horizontal"
        android:src="@drawable/tab_selected_arrow" />
</item>

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

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