android-如何为LinearLayout设置2 xml drawable

时间:2022-11-21 14:45:52

I set a xml drawable for this LinearLayout and I want to add another one to it . this is the first xml drawable :

我为这个LinearLayout设置了一个xml drawable,我想为它添加另一个。这是第一个xml drawable:

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

    <item android:drawable="@color/blue" android:state_pressed="true"/>
    <item android:drawable="@color/blue" android:state_selected="true"/>
    <item android:drawable="@color/white"/>

</selector>

This is xml layout :

这是xml布局:

    <?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:background="@drawable/bg1"
    android:orientation="vertical" >

at this step ,I want to add a border around this LinearLayout , I've created another drawable layout . this is the code :

在这一步,我想在这个LinearLayout周围添加一个边框,我创建了另一个可绘制的布局。这是代码:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF"/>
    <stroke android:width="3dip" android:color="#B1BCBE" />
    <corners android:radius="10dip"/>
    <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
</shape>

How can I use these 2 xmles for one element ?

如何将这2个xmles用于一个元素?

1 个解决方案

#1


1  

You can not apply two drawable to a layout but you can achieve your requirement using single drawable only.

您不能将两个drawable应用于布局,但只能使用单个drawable来实现您的要求。

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

<item android:drawable="@color/blue" android:state_pressed="true"/>
<item android:drawable="@color/blue" android:state_selected="true"/>
<item android:drawable="@drawable/name_of_your_second_drawable"/>

</selector>

#1


1  

You can not apply two drawable to a layout but you can achieve your requirement using single drawable only.

您不能将两个drawable应用于布局,但只能使用单个drawable来实现您的要求。

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

<item android:drawable="@color/blue" android:state_pressed="true"/>
<item android:drawable="@color/blue" android:state_selected="true"/>
<item android:drawable="@drawable/name_of_your_second_drawable"/>

</selector>