发布于 2017-09-15 11:25:12 | 118 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android selector的实例详解的相关资料,这里提供实例帮助大家理解掌握这部分知识,需要的朋友可以参考下

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 开发的文章还有很多,欢迎大家搜索查阅,谢谢阅读,希望能帮助到大家,谢谢大家对本站的支持!



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务