以编程方式从可绘制文件夹中设置按钮背景会使按钮背景变黑

时间:2015-01-20 14:13:27

标签: android xml android-drawable

我正在为gridview创建dyanmically按钮,但是我想应用下面提供的可绘制背景。但是背景为黑色,但按下时显示正确。

我创建了一个名为roundedge_button_dark.xml的按钮:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
    <shape android:shape="rectangle"  >
        <corners android:radius="6dip" />
        <gradient android:angle="-90" android:startColor="#3f3f3f" android:endColor="#a6a6a6"  />                   
    </shape>
</item>
<item android:state_focused="true">
    <shape android:shape="rectangle"  >
        <corners android:radius="6dip" />
        <solid android:color="#ff0000"/>       
    </shape>
</item>  
<item>
    <shape android:shape="rectangle"  >
        <corners android:radius="6dip" /> 
        <gradient android:angle="-90" android:startColor="#0F0F0F" android:endColor="#000000" />            
    </shape>
</item>
</selector>

我像这样访问所说的可绘制按钮:

button.setBackgroundResource(R.drawable.roundedge_button_dark);

我遇到的问题是图像显示黑色,这是非状态定义项目的最终颜色;我错过了什么或者这不可能?

2 个答案:

答案 0 :(得分:0)

试试这个,

<item android:state_pressed="true">
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <stroke android:width="2dp" android:color="#ff0000" />

        <solid android:color="#ffff00" />

        <padding android:bottom="2dp" android:left="5dp" android:right="5dp" android:top="2dp" />

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

    </item>
<item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <stroke android:width="2dp" android:color="#ff0000" />

        <solid android:color="#FF6699" />

        <padding android:bottom="2dp" android:left="5dp" android:right="5dp" android:top="2dp" />

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

    </item>

答案 1 :(得分:0)

或尝试这个, 首先为按钮状态创建可绘制的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Button Pressed-->
    <item   android:state_focused="false"
            android:state_pressed="true"
            android:drawable="@drawable/rounded_button_click_effect"
        />
    <!-- Button Default Image-->
    <item   android:drawable="@drawable/rounded_button"/>
</selector>

之后,在按钮点击的情况下创建round_button_click_effect.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" >
            <corners android:radius="6dip" />
            <gradient android:angle="-90" android:startColor="#3f3f3f" android:endColor="#a6a6a6"  />
        </shape>
    </item>
</layer-list>

最后在默认状态下创建round_button.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" >
            <corners android:radius="6dip" />
            <gradient android:angle="-90" android:startColor="#0F0F0F" android:endColor="#000000" />
        </shape>
    </item>
</layer-list>