Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用可拉伸和<;形状>;在<;项目>;XML_Android - Fatal编程技术网

Android 使用可拉伸和<;形状>;在<;项目>;XML

Android 使用可拉伸和<;形状>;在<;项目>;XML,android,Android,是否可以将XML后台资源与drawable和属性一起使用 我有这个按钮 <Button android:layout_marginRight="5dp" android:id="@+id/send_button" android:layout_width="40dp" android:layout_height="40dp" android:backgro

是否可以将XML后台资源与drawable和
属性一起使用

我有这个按钮

        <Button
            android:layout_marginRight="5dp"
            android:id="@+id/send_button"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/send_button" />

它有一个后台send_button.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/ic_send_white_48dp" android:state_focused="true"/>
    <item android:drawable="@drawable/ic_send_white_48dp" android:state_pressed="true"/>
    <item android:drawable="@drawable/ic_send_black_48dp"/>

</selector>

现在这个很好用。但我还想在可绘制的圆角后面添加背景色,如下所示:

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

    <corners android:radius="4dp" />

    <gradient
        android:angle="270"
        android:endColor="#88b823"
        android:startColor="#b0dc54" />

</shape>

那么,有可能将这两种XML资源结合起来吗? 到目前为止,我尝试的只是显示可绘制的,而不是形状:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/ic_send_white_48dp" android:state_focused="true"/>
    <item android:drawable="@drawable/ic_send_white_48dp" android:state_pressed="true"/>
    <item android:drawable="@drawable/ic_send_black_48dp">
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
            <corners android:radius="4dp" />

            <gradient android:angle="270" 
                android:endColor="#88b823" 
                android:startColor="#b0dc54" />
        </shape>
    </item>

</selector>

感谢@commonware为我指明了正确的方向。这是工作代码(目前仅适用于默认状态):



尝试将位图和形状组合成一个图形,然后将其用作背景。谢谢,这似乎很好。你能不能把这个贴出来作为答案,这样我就可以接受了。我有一个
,有两个
-属性,第一个属性是
,第二个属性是
。实际上,你为什么不回答你自己的问题,这样你就可以提供更多你使用的代码。这对将来看到这个问题的人会更有用。
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/ic_send_white_48dp" android:state_focused="true"/>
    <item android:drawable="@drawable/ic_send_white_48dp" android:state_pressed="true"/>
    <item>
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
            <item>
                <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
                    <corners android:radius="4dp" />
                    <solid android:color="@color/action_bar" />
                </shape>
            </item>
            <item>
                <bitmap android:src="@drawable/ic_send_black_48dp" />
            </item>
        </layer-list>
    </item>

</selector>