我可以在一个Android drawable中使用多个形状吗?

Dav*_*own 18 layout android drawable

我要做的是定义一个在LinearLayout中使用的常用背景,它经常在我的应用程序的许多活动中使用.此特定布局是显示在每个活动顶部的标题.

我要做的是创建一个drawable,用渐变填充linearlayout并在渐变下面有一条水平线.

有没有人知道这是否可行,或者我是否必须使用嵌套布局来做这种事情.

我对drawable xml的尝试是

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#AA000000" android:endColor="#AA333333"
        android:angle="270" />
</shape>
</item>
<item>
<shape android:shape="line">
    <stroke android:width="3dp" android:color="#FFFFFFFF"
            android:dashWidth="1dp" android:dashGap="2dp" />
    <size android:height="5dp" />
</shape>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)

Fra*_*nco 32

Drawable接受多个形状,(总是在其他文件中定义)如果我理解你的问题,也许你可以做一个Layer drawable(这会在底层绘制多个底层drawable)

我在这里写这个,所以,我没有测试过,但尝试这个并阅读这篇精彩的文档.

<?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/shape_7"/>
        <item android:drawable="@drawable/shape_1"/>
    </layer-list>
Run Code Online (Sandbox Code Playgroud)

android完整的xml资源

干杯

  • 是否也可以将它们设置在另一个之下(在Y坐标中)? (27认同)

小智 5

使用重力layer-list 中设置您的项目

    垂直方向使用:
    机器人:重力=“顶部”
    机器人:重力=“中心”
    机器人:重力=“底部”
    水平方向使用:
    机器人:重力=“左”
    机器人:重力=“中心”
    机器人:重力=“正确”
<layer-list
           xmlns:android="http://schemas.android.com/apk/res/android">
          <item>
                <bitmap
                   android:src="@drawable/top_img"
                   android:gravity="top"/>
          </item>
          <item>
                <bitmap
                   android:src="@drawable/center_img"
                   android:gravity="center"/>
          </item>
          <item>
                <bitmap
                   android:src="@drawable/bottom_img"
                   android:gravity="bottom"/>
          </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

确保父元素有足够的空间来容纳所有项目! 否则它们会相互重叠