如何在Android中设置按钮选择颜色以及圆角?

栏目: IOS · Android · 发布时间: 7年前

内容简介:http://stackoverflow.com/questions/12427107/how-to-set-button-selection-color-along-with-rounded-corners-in-android

我想在android中为一个按钮设置一个圆角,同时在选择时更改按钮颜色.我在做以下事情

绘制/ 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>

绘制/ 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"

这里,问题在于,当选择&取消.但圆角不行.

怎么做?

如果我使用

android:background="@drawable/push_button_background"

然后,圆角正在工作,但按钮颜色在选择上的更改不起作用

如何实现?

我已经介绍了 this 的链接.即使没有帮助!

我已经找到答案我的问题,几个审判&错误尝试.

这是解决方案.

<?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>

http://stackoverflow.com/questions/12427107/how-to-set-button-selection-color-along-with-rounded-corners-in-android


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

PHP for the World Wide Web, Second Edition (Visual QuickStart Gu

PHP for the World Wide Web, Second Edition (Visual QuickStart Gu

Larry Ullman / Peachpit Press / 2004-02-02 / USD 29.99

So you know HTML, even JavaScript, but the idea of learning an actual programming language like PHP terrifies you? Well, stop quaking and get going with this easy task-based guide! Aimed at beginning ......一起来看看 《PHP for the World Wide Web, Second Edition (Visual QuickStart Gu》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具