问题描述
我想在android中为按钮设置圆角,并在选择时更改按钮颜色.我正在做以下事情.
I want to set a rounded corner for a button in android along with changing the button color on when selected. I am doing the following things.
drawable/push_button.xml
drawable/push_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@color/green"/>
<item android:state_focused="true" android:drawable="@color/green"/>
<item android:state_focused="false" android:drawable="@color/green"/>
<item android:state_pressed="false" android:drawable="@color/red"/>
<item android:drawable="@drawable/push_button_background"/>
</selector>
drawable/push_button_background.xml
drawable/push_button_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
>
<solid android:color="#3F4040"/>
<corners
android:radius="7dp"
/>
</shape>
在代码中,我正在使用
android:background="@drawable/push_button"
在这里,问题是,在选择&时,按钮的颜色设置正确.取消选择.但是,圆角不起作用.
Here, the problem is, button colors are setting properly when selected & deselected. But, rounded corners are not working.
该怎么做?如果我使用
How to do that?If I use
android:background="@drawable/push_button_background"
然后,圆角起作用了,但是选择时按钮颜色的改变不起作用
then, rounded corners are working but the button color change on selection is not working
如何实现呢?
我已引用此链接.即使那样也没有帮助!
I have referred this link. Even then no help!!
推荐答案
我通过很少的试验和发现就找到了我的问题的答案.错误尝试.
I have found answer for my question with few trial & error attempts.
这是解决方案.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true">
<shape >
<solid android:color="@color/green"/>
<corners
android:radius="7dp"/>
</shape>
</item>
<item android:state_focused="true" >
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@color/green"/>
<corners
android:radius="7dp"/>
</shape>
</item>
<item android:state_focused="false" >
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@color/red"/>
<corners
android:radius="7dp"/>
</shape>
</item>
<item android:state_pressed="false" >
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@color/red"/>
<corners
android:radius="7dp"
/>
</shape>
</item>
</selector>
这篇关于如何在Android中设置按钮选择颜色以及圆角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!