



下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第AndroidButton按钮点击背景和文字变化操作Android原生的按钮点击状态是有变化的,但是如果是自己加了一个.png格式的图片为背景色,按钮点击就不会有任何效果,为了达到点击按钮有一闪的效果,我们就需要准备两张图进行切换,而且文字也要变色,老规矩废话不多说直接上代码:
按钮背景图片放在drawable/background_button.xml
xmlversion=1.0encoding=utf-8
selectorxmlns:android=/apk/res/android
itemandroid:drawable=@drawable/bg_pressandroid:state_pressed=true/
itemandroid:drawable=@drawable/bg_normalandroid:state_enabled=true/
itemandroid:drawable=@drawable/bg_normal/
/selector
准备两张图片一张为bg_press.png,一张为bg_normal.png。
在需要变化的按钮中设置:
Button
android:layout_width=wrap_content
android:layout_height=wrap_content
android:layout_alignParentRight=true
android:layout_centerVertical=true
android:layout_marginRight=50dp
android:focusable=false
android:gravity=center
android:textSize=24px
android:text=@string/str_tethering_modify
android:background=@drawable/background_button/
这有背景色变化就解决完了,下面到按钮上的文字了,现在点击按钮按钮上的文字是没有变化的,为了达到按钮文字颜色的变化我们再新建一个xml文件。
按钮颜色变化drawable/button_color.xml
xmlversion=1.0encoding=utf-8
selectorxmlns:android=/apk/res/android
itemandroid:state_pressed=trueandroid:color=#975508/
itemandroid:state_focused=falseandroid:state_pressed=falseandroid:color=#E5960E/
itemandroid:state_focused=trueandroid:color=#975508/
itemandroid:state_focused=falseandroid:color=#E5960E/
/selector
加入到我们的按钮textColor中
Button
android:layout_width=wrap_content
android:layout_height=wrap_content
android:layout_alignParentRight=true
android:layout_centerVertical=true
android:layout_marginRight=50dp
android:focusable=false
android:gravity=center
android:textSize=24px
android:textColor=@drawable/button_color
android:text=@string/str_tethering_modify
android:background=@drawable/background_button/
这样直接使用背景和文字就都有点击效果啦,但是如果有这样一个需求,在某些条件下需要再设置按钮文字的颜色button.setTextColor(color),这样设置完后,发现我们按钮上文字点击又没有变化了,我之前试着直接button.setTextColor(R.drawable.button_color);发现这样是没有任何用处的。这样就需要使用ColorStateList来解决,顾名思义,就是定义颜色的状态列表,通过监听按钮不同状态来设置不同的颜色,
老规矩,废话不多说了,直接贴代码:
*按钮点击颜色变化
privateColorStateListcolorStateList;
colorStateList=(ColorStateList)getResources().getColorStateList(R.drawable.button_color);
if(xxx){
button.setTextColor(Color.White);
}else{
button.setTextColor(colorStateList);
}
这样就完美解决了按钮点击状态的变化啦。
补充知识:androidstudio设置按钮和背景融为一体也就是按钮去除阴影
Button
android:layout_width=wrap_content
android:layout_height=wrap_con
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 信息技术课堂教案七年级下册
- 新质生产力指导下光伏支架结构设计与抗风能力评估
- 2025年度员工社保自愿放弃与代缴服务全面协议
- 2025年智能家居系统采购诚意金缴纳标准合同范本(专业版)
- 2025年废钢铁清洁运输及资源化利用合作协议
- 2025年茶餐厅与供应商农产品直供合作协议
- 餐厅消防安全检查清单
- 2025年度国际货物进出口仲裁服务协议书
- 跨界合作-2025年度虚拟现实与增强现实技术应用投资联营合同
- 二年级数学应用题分类训练汇编
- 水稻植保无人机服务协议
- 读后续写体育竞技个人成长课件高三英语二轮复习
- 箱式变电站技术规范书
- 有轨电车交通工程设施设计规范
- 施工安全村民告知书
- 快速入门穿越机-让你迅速懂穿越机
- 广州南方学院(原中山大学南方学院)学校办公室新闻宣传中心新闻管理岗招聘公开引进高层次人才和急需紧缺人才笔试参考题库(共500题)答案详解版
- 儿童呼吸机基本使用
- 起重机械安全日管控、周排查、月调度制度
- 派出所民警心理健康辅导
- 民事诉讼法课件
评论
0/150
提交评论