Android中的Selector的使用總結

來源:互聯網
上載者:User

標籤:

Android中的Selector主要是用來改變ListView和Button控制項等其他空的預設背景,其使用方法可以如下所示:

1、首先在res目錄下drawable檔案夾,建立一個comm_button_bg.xml的xml檔案。

2、編輯comm_button_bg.xml檔案,其代碼結構如下所示:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"
<!-- 觸摸模式下單擊時的背景圖片-->  
<item android:drawable="@drawable/btn_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="@drawable/btn_bg_normal" android:state_pressed="false"></item> <!-- 非觸摸模式下獲得焦點並單擊時的背景圖片 -->
</selector>

3、可以根據項目需求自訂自己想要的樣式,如下所示:

設定選擇按鈕按下以及不按下的  文字顏色變化效果  textColor<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">     <!--設定點擊的時候,控制項的文本顏色--->    <item android:color="@android:color/white" android:state_pressed="true"></item>    <item android:color="@android:color/black" android:state_pressed="false"></item></selector>

類似的屬性還可以為:

android:state_selected選中

android:state_focused獲得焦點
android:state_pressed點擊
android:state_enabled設定是否響應事件,指所有事件

4、在布局檔案中 使用comm_button_bg.xml定義好的樣式

 <Button        android:background="@drawable/common_button_bg"    <!----使用自訂好的樣式----->         android:text="測試"        android:textColor="@drawable/common_button_textcolor_selector"        android:textSize="20sp" />

 

Android中的Selector的使用總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.