按钮看起来没有激活

时间:2014-11-03 04:30:28

标签: android xml

我希望一个按钮从一开始就显示为激活(透明)。 我的选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" >
    <shape>
        <solid
            android:color="@android:color/transparent" />
    </shape>
</item>
<item android:state_pressed="false" android:state_focused="false" android:state_activated="false">
    <shape>
        <gradient
            android:startColor="#ef4444"
            android:endColor="#992f2f"
            android:angle="270" />
    </shape>
</item>

按钮的代码:

<Button
    android:background="@drawable/my_bg"
    android:state_activated="true"
/>

按钮的默认颜色是红色,所以我猜问题是在android:state_activated =“true”。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

请使用它,它会帮助你

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

    <item android:state_focused="true"><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
            <solid android:color="#2d7ebb" />

            <corners android:radius="5dp">
                <stroke android:width="2dip" android:color="#2d7ebb" />
            </corners>
        </shape></item>
    <item android:state_pressed="true"><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
            <solid android:color="#2d7ebb" />

            <corners android:radius="5dp">
                <stroke android:width="2dip" android:color="#2d7ebb" />
            </corners>
        </shape></item>
    <item><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
            <solid android:color="#55acee" />

            <stroke android:width="2dip" android:color="#55acee" />

            <corners android:radius="5dp" />
        </shape></item>

</selector>