当前位置 : 主页 > 手机开发 > android >

Android 中图片和按钮按下状态变化实例代码解析

来源:互联网 收集:自由互联 发布时间:2021-06-05
1、图片设置背景选择器,以便点按或设置选中与否,背景切换 res/drawable/selector_settings_item_back.xml xml version="1.0" encoding="utf-8" selector xmlns:android="http://schemas.android.com/apk/res/android" item andr

1、图片设置背景选择器,以便点按或设置选中与否,背景切换

res/drawable/selector_settings_item_back.xml 

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
  <item android:state_focused="true" android:drawable="@color/settingsSelectedItem"/> 
  <item android:state_pressed="true" android:drawable="@color/settingsSelectedItem"/> 
  <item android:state_selected="true" android:drawable="@color/settingsSelectedItem"/> 
  <item android:state_focused="false" android:drawable="@color/settingsItem"/> 
</selector > 

颜色值定义:

res/values/colors.xml 

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
  <color name="settingsItem">#ffffff</color> 
  <color name="settingsSelectedItem">#FFA500</color> 
</resources> 

2、圆角按钮,按下抬起切换背景,同时切换文字颜色

res/layout/activity_xxx.xml 

<Button 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_weight="1" 
android:background="@drawable/selector_shape_corner_button" 
  android:text="审批中" android:textColor="@drawable/selector_font_style_corner_button" 
  android:textSize="13sp" 
  /> 

其中引用了 res/drawable/ 下的两个 selector ,

一个是背景图片随点按抬起状态进行切换,一个是文本颜色随点按抬起进行切换。

res/drawable/selector_shape_corner_button.xml 

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
  <item android:state_focused="true" android:drawable="@drawable/shape_corner_button_fill"/> 
  <item android:state_pressed="true" android:drawable="@drawable/shape_corner_button_fill"/> 
  <item android:state_selected="true" android:drawable="@drawable/shape_corner_button_fill"/> 
  <item android:state_focused="false" android:drawable="@drawable/shape_corner_button"/> 
</selector >
 res/drawable/selector_font_style_corner_button 

shape_corner_button.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
  android:shape="rectangle"> 
  <corners 
    android:radius="5dp" /> 
  <solid 
    android:color="#001da1f2" /> 
  <stroke 
    android:width="1dp" 
    android:color="#1da1f2" /> 
</shape> 

shape_corner_button_fill.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape 
xmlns:android="http://schemas.android.com/apk/res/android" 
  android:shape="rectangle"> 
  <corners 
    android:radius="5dp" /> 
  <solid 
    android:color="#ff1da1f2" /> 
  <stroke 
    android:width="1dp" 
    android:color="#1da1f2" /> 
</shape> 
<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
  <item android:state_focused="true" android:color="#ffffff"/> 
  <item android:state_pressed="true" android:color="#ffffff"/> 
  <item android:state_selected="true" android:color="#ffffff"/> 
  <item android:state_focused="false" android:color="#1da1f2"/> 
</selector > 

总结

以上所述是小编给大家介绍的Android 中图片和按钮按下状态变化实例代码解析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对自由互联网站的支持!

网友评论