溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android中的selector怎么使用

發(fā)布時(shí)間:2022-04-11 16:02:51 來源:億速云 閱讀:185 作者:iii 欄目:編程語言

本篇內(nèi)容主要講解“Android中的selector怎么使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Android中的selector怎么使用”吧!

Android  selector的詳解

前言:

StateListDrawable 是一種通過XML文件來定義的drawable,使用幾個(gè)不同的圖片來呈現(xiàn)同一個(gè)圖形,通過object的狀態(tài)來實(shí)現(xiàn)切換。例如,一個(gè)Button有幾個(gè)不同的狀態(tài)(按壓,獲取焦點(diǎn)等等),這種情況下,通過使用 state list drawable,你就可以實(shí)現(xiàn)在不同的狀態(tài)下使用不同的背景圖片。

你可以在一個(gè)XML文件中描述state list。通過在根節(jié)點(diǎn)selector下定義一個(gè)item元素來添加每個(gè)圖形。每一各item中使用不同的狀態(tài)屬性來定義不用的drawable。

當(dāng)每一次狀態(tài)改變的時(shí)候,state list都會(huì)從上到下被遍歷一遍,第一個(gè)與當(dāng)前state相匹配的item將會(huì)被使用—- 這個(gè)選擇并不是作出“最匹配”結(jié)果,而是簡(jiǎn)單的找到第一個(gè)匹配的狀態(tài)。

selector一般都是用來作為有狀態(tài)改變的View的背景,以此來達(dá)到當(dāng)用戶對(duì)View進(jìn)行操作,導(dǎo)致View狀態(tài)改變時(shí),作出改變,讓用戶感知View的狀態(tài)變化。

官方說明

文件位置: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>

更多詳細(xì)說明,請(qǐng)查閱xsoftlab

實(shí)際使用

下面做一個(gè)簡(jiǎn)單的實(shí)例,對(duì)Button的背景根據(jù)狀態(tài)做一下處理

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>

到此,相信大家對(duì)“Android中的selector怎么使用”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI