android Button美化

本文详细介绍了在Android应用中创建按钮的四种不同状态样式(启用、聚焦、按压及混合状态),通过XML配置文件实现了按钮在不同状态下的视觉变化,并展示了如何在布局文件中应用这些样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

四个配置文件: 放入res/drawable目录中:

android:state_enabled="" 是否有效
android:state_focused=""   是否聚焦
android:state_pressed=""   是否被按下

button_bg.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">

  3.     <item           
  4.     android:state_pressed="true"   
  5.     android:drawable="@drawable/button_focusedandpressed" />
  6.     <item
  7.        android:state_focused="true"
  8.        android:state_pressed="true"
  9.        android:drawable="@drawable/button_focusedandpressed" />
  10.     <item
  11.        android:state_focused="false"
  12.        android:state_pressed="true"
  13.        android:drawable="@drawable/button_defocused" />
  14.     <item
  15.        android:state_focused="true"
  16.        android:state_pressed="false"
  17.        android:drawable="@drawable/button_focused" />
  18.     <item
  19.        android:state_focused="false"
  20.        android:drawable="@drawable/button_defocused" />
  21. </selector>
复制代码

button_defocused.xml //实现渐变色的效果(焦点不在button上,且未按下)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">
  3. <!-- 颜色渐变效果 -->
  4. <gradient
  5. android:startColor="#00FF00"
  6. android:endColor="#C0C0C0"
  7. android:paddingTop="5dp"
  8. android:angle="0" />
  9. <!-- 按钮四个角的平滑度 -->
  10. <corners android:radius="15dp" />
  11. </shape>
复制代码

button_focused.xml (焦点在button上,但没有按下)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">
  3. <!-- 颜色渐变效果 -->
  4. <gradient
  5. android:startColor="#505050"
  6. android:endColor="#C0C0C0"
  7. android:paddingTop="5dp"
  8. android:angle="0" />

  9. <!-- 按钮四个角的平滑度 -->
  10. <corners android:radius="15dp" />

  11. <!-- 焦点在按钮上时光环的宽度与颜色 -->
  12. <stroke android:width="4dp" android:color="#FFFF9200" />

  13. </shape>
复制代码

button_focusedandpressed.xml (按钮按下时,或被点击时)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">
  3. <!-- 颜色渐变效果 -->
  4. <gradient
  5. android:startColor="#FFFF00"
  6. android:endColor="#C0C0C0"
  7. android:paddingTop="5dp"
  8. android:angle="0" />

  9. <!-- 按钮四个角的平滑度 -->
  10. <corners android:radius="20dp" />

  11. <!-- 焦点在按钮上时光环的宽度与颜色 -->
  12. <stroke android:width="4dp" android:color="#FFFF9200" />

  13. </shape>
复制代码




布局文件中的用法;

<Button

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="@drawable/button_bg“

android:textSize="30dip"

android:id="@+id/button_cal"

android:text="电话控制"/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值