当前位置: 移动技术网 > 移动技术>移动开发>Android > Android selector的实例详解

Android selector的实例详解

2019年07月24日  | 移动技术网移动技术  | 我要评论

android  selector的详解

前言:

statelistdrawable 是一种通过xml文件来定义的drawable,使用几个不同的图片来呈现同一个图形,通过object的状态来实现切换。例如,一个button有几个不同的状态(按压,获取焦点等等),这种情况下,通过使用 state list drawable,你就可以实现在不同的状态下使用不同的背景图片。

你可以在一个xml文件中描述state list。通过在根节点selector下定义一个item元素来添加每个图形。每一各item中使用不同的状态属性来定义不用的drawable。

当每一次状态改变的时候,state list都会从上到下被遍历一遍,第一个与当前state相匹配的item将会被使用—- 这个选择并不是作出“最匹配”结果,而是简单的找到第一个匹配的状态。

selector一般都是用来作为有状态改变的view的背景,以此来达到当用户对view进行操作,导致view状态改变时,作出改变,让用户感知view的状态变化。

官方说明

文件位置:res/drawable/filename.xml

编译资源类型:statelistdrawable

资源引用:

in java: r.drawable.filename
in xml: @[package:]drawable/filename

语法:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
  android:constantsize=["true" | "false"]
  android:dither=["true" | "false"]
  android:variablepadding=["true" | "false"] >
  <item
    android:drawable="@[package:]drawable/drawable_resource"
    android:state_pressed=["true" | "false"]
    android:state_focused=["true" | "false"]
    android:state_hovered=["true" | "false"]
    android:state_selected=["true" | "false"]
    android:state_checkable=["true" | "false"]
    android:state_checked=["true" | "false"]
    android:state_enabled=["true" | "false"]
    android:state_activated=["true" | "false"]
    android:state_window_focused=["true" | "false"] />
</selector>

更多详细说明,请查阅xsoftlab

实际使用

下面做一个简单的实例,对button的背景根据状态做一下处理

xml文件

selector_ts.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@color/pink" android:state_pressed="true" />
  <item android:drawable="@color/yellow" android:state_selected="true" />
  <item android:drawable="@drawable/shaperect" android:state_enabled="false" />
  <item android:drawable="@color/stone" android:state_enabled="true" />
</selector>

主布局文件(activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:focusable="true"
  android:focusableintouchmode="true"
  android:orientation="vertical"
  android:padding="10dp"
  tools:context="mraz.com.tabdemo.mainactivity">

  <button
    android:id="@+id/bt_content"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:background="@drawable/selector_ts" />

  <linearlayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margintop="50dp"
    android:orientation="horizontal">


    <button
      android:id="@+id/bt_selected"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="select"
      android:textallcaps="false" />

    <button
      android:id="@+id/bt_disable"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="disable"
      android:textallcaps="false" />

    <button
      android:id="@+id/bt_pressed"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="press"
      android:textallcaps="false" />
  </linearlayout>
</linearlayout>

代码部分 比较简单,这里就不占用过多的篇幅了,看下简单的效果,大家应该就知道如何编写小小的activity了。

实际效果

如有疑问请留言或者到本站社区交流讨论,本站关于android 开发的文章还有很多,欢迎大家搜索查阅,谢谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网