Android选择器状态选择器的使用详解

  

<强>一、目的效果

  

,,,,,,越好的用户体验来源更直接更明显的事件反馈.selector可以“预存”多种响应的反馈,主要以下多种状态有:

  
  

android: state_selected是选中
  android: state_focused是获得焦点
  android: state_pressed是点击
  android: state_enabled是设置是否响应事件,指所有事件
  

     

,,,,,,设置不同状态的表现形式,则会在不同场景下有不同状态。如文字:被选中状态,未被选中状态。

  

,,,,,,选择器的普通使用则是为对应单个控件添加以选择为背景的资源,则能达到目的。联合使用则是基本使用一种升级。在我们的导航栏中,常使用LinearLayout或者RelativeLayout包含一个ImageView和一个TextView。图片用于直观观感,文字用于更清晰的描述。

  

,,,,,在一个整体菜单被选中时,需要图片及文字都表现对应的状态。并为保证较大的事件响应范围,点击事件常赋予包含图片和文字的父控件。即:为LinearLayout设置点击事件,ImageView, TextView表现对应的状态。

  

<强>二,具体实现

  

文字的选择器:res添加目录颜色,res/颜色/bg_tv_selector.xml

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt;选择xmlns: android=" http://schemas.android.com/apk/res/android "比;   & lt;项目android:颜色=" @color/红”android: state_pressed=" true "/比;   & lt;项目android:颜色=癅color/黑色”/比;   & lt;/selector>      

图片的选择器:bg_qq_iv_selector.xml

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt;选择xmlns: android=" http://schemas.android.com/apk/res/android "比;   & lt;项目android:可拉的=" @mipmap/b_qq_pressed”android: state_pressed=" true "/比;   & lt;项目android:可拉的=癅mipmap/b_qq”/比;   & lt;/selector>      

使用形状为按钮的背景图,并设置选择器:
  bg_bt_drawable_normal.xml:

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt;形状xmlns: android=" http://schemas.android.com/apk/res/android "比;   & lt;角落android:半径=" 10 dp/比;   & lt;中风   android:宽度=" 2 dp "   android:颜色=癅color/黑色”/比;   & lt;/shape>      之前      

bg_bt_drawable_pressed.xml:

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt;形状xmlns: android=" http://schemas.android.com/apk/res/android "比;   & lt;角落android:=" 5 dp/半径比;   & lt;中风   android:宽度=" 2 dp "   android:颜色=" @color/蓝”   android: dashGap=" 10 dp/比;   & lt;梯度   android: centerColor=" @color/红”   android: endColor=癅color/绿色/比;   & lt;/shape>      

bg_bt_selector.xml:

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt;选择xmlns: android=" http://schemas.android.com/apk/res/android "比;   & lt;项目android:可拉的=" @drawable/bg_bt_drawable_pressed”android: state_pressed=" true "/比;   & lt;项目android:可拉的=癅drawable/bg_bt_drawable_normal”/比;   & lt;/selector>      

activity_main.xml中使用:

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt; LinearLayout xmlns: android=" http://schemas.android.com/apk/res/android "   xmlns:工具=" http://schemas.android.com/tools "   android: layout_width=" match_parent "   android: layout_height=" match_parent "   android:取向=按怪薄?   工具:上下文=癱om.future.selectorlldemo.MainActivity”比;      LinearLayout & lt;   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   面向android:="水平"比;      LinearLayout & lt;   android: id=癅 + id/qq_ll”   android: layout_width=" 0 dp”   android: layout_height=" wrap_content "   android: layout_weight=" 1 "   android:背景=" @color/绿色”   android:点击=" true "   android:重力="中心"   面向android:=按怪薄北?      & lt; ImageView   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android: src=" https://www.yisu.com/zixun/@drawable bg_qq_iv_selector "/比;      & lt; TextView   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android:文本=癚Q”   输入textColor=癅color/bg_tv_selector”android:/比;      & lt;/LinearLayout>      LinearLayout & lt;   android: id=癅 + id/weixin_ll”   android: layout_width=" 0 dp”   android: layout_height=" wrap_content "   android: layout_weight=" 1 "   android:背景=" @color/蓝”   android:点击=" true "   android:重力="中心"   面向android:=按怪薄北?      & lt; ImageView   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android: src=" https://www.yisu.com/zixun/@drawable bg_weixin_iv_selector "/比;      & lt; TextView   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android:文本="微信"   输入textColor=癅color/bg_tv_selector”android:/比;      & lt;/LinearLayout>   & lt;/LinearLayout>      LinearLayout & lt;   android: id=癅 + id/text_button_ll”   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   面向android:="水平"比;      & lt; TextView   android: layout_width=" 0 dp”   android: layout_height=" wrap_content "   android: layout_weight=" 1 "   android:文本="文字和按钮”   输入textColor=癅color/bg_tv_selector”android:/比;      & lt;按钮   android: layout_width=" 0 dp”   android: layout_height=" wrap_content "   android: layout_weight=" 1 "   android:背景=" @drawable/bg_bt_selector”   android:点击=" false "   android:文本="确认“/比;      & lt;/LinearLayout>   & lt;/LinearLayout>

Android选择器状态选择器的使用详解