一、目的效果
公司主營業(yè)務(wù):成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。成都創(chuàng)新互聯(lián)公司是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)公司推出岳陽縣免費(fèi)做網(wǎng)站回饋大家。
越好的用戶體驗(yàn)來源更直接更明顯的事件反饋。selector可以“預(yù)存”多種響應(yīng)的反饋,主要以下多種狀態(tài)有:
android:state_selected是選中
android:state_focused是獲得焦點(diǎn)
android:state_pressed是點(diǎn)擊
android:state_enabled是設(shè)置是否響應(yīng)事件,指所有事件
設(shè)置不同狀態(tài)的表現(xiàn)形式,則會(huì)在不同場景下有不同狀態(tài)。如文字:被選中狀態(tài),未被選中狀態(tài)。
selector的普通使用則是為對(duì)應(yīng)單個(gè)控件添加以selector為背景的資源,則能達(dá)到目的。聯(lián)合使用則是基本使用一種升級(jí)。在我們的導(dǎo)航欄中,常使用LinearLayout或者RelativeLayout包含一個(gè)ImageView和一個(gè)TextView。圖片用于直觀觀感,文字用于更清晰的描述。
在一個(gè)整體菜單被選中時(shí),需要圖片及文字都表現(xiàn)對(duì)應(yīng)的狀態(tài)。并為保證較大的事件響應(yīng)范圍,點(diǎn)擊事件常賦予包含圖片和文字的父控件。即:為LinearLayout設(shè)置點(diǎn)擊事件,ImageView、TextView表現(xiàn)對(duì)應(yīng)的狀態(tài)。
二、具體實(shí)現(xiàn)
文字的selector:res添加目錄color,res/color/bg_tv_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/red" android:state_pressed="true" /> <item android:color="@color/black" /> </selector>
圖片的selector:bg_qq_iv_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@mipmap/b_qq_pressed" android:state_pressed="true" /> <item android:drawable="@mipmap/b_qq" /> </selector>
使用shape為Button的背景圖,并設(shè)置selector:
bg_bt_drawable_normal.xml:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="10dp" /> <stroke android:width="2dp" android:color="@color/black" /> </shape>
bg_bt_drawable_pressed.xml:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="5dp" /> <stroke android:width="2dp" android:color="@color/blue" android:dashGap="10dp" /> <gradient android:centerColor="@color/red" android:endColor="@color/green" /> </shape>
bg_bt_selector.xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/bg_bt_drawable_pressed" android:state_pressed="true" /> <item android:drawable="@drawable/bg_bt_drawable_normal" /> </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:orientation="vertical" tools:context="com.future.selectorlldemo.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <LinearLayout android:id="@+id/qq_ll" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@color/green" android:clickable="true" android:gravity="center" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/bg_qq_iv_selector" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="QQ" android:textColor="@color/bg_tv_selector" /> </LinearLayout> <LinearLayout android:id="@+id/weixin_ll" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@color/blue" android:clickable="true" android:gravity="center" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/bg_weixin_iv_selector" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="WeChat" android:textColor="@color/bg_tv_selector" /> </LinearLayout> </LinearLayout> <LinearLayout android:id="@+id/text_button_ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="文字和Button" android:textColor="@color/bg_tv_selector" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/bg_bt_selector" android:clickable="false" android:text="確認(rèn)" /> </LinearLayout> </LinearLayout>
MainActivity.Java中應(yīng)用效果:
public class MainActivity extends AppCompatActivity implements View.OnClickListener { /** * qq登錄按鈕 */ private LinearLayout qqLoginLL; /** * 微信登錄按鈕 */ private LinearLayout weixinLoginLL; /** * 文字和Button一起 */ private LinearLayout textButtonLL; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); qqLoginLL = (LinearLayout) findViewById(R.id.qq_ll); weixinLoginLL = (LinearLayout) findViewById(R.id.weixin_ll); textButtonLL = (LinearLayout) findViewById(R.id.text_button_ll); qqLoginLL.setOnClickListener(this); weixinLoginLL.setOnClickListener(this); textButtonLL.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.qq_ll: Toast.makeText(MainActivity.this, "你點(diǎn)擊了QQ登錄區(qū)間", Toast.LENGTH_SHORT).show(); break; case R.id.weixin_ll: Toast.makeText(MainActivity.this, "你點(diǎn)擊了WeChat登錄區(qū)間", Toast.LENGTH_SHORT).show(); break; case R.id.text_button_ll: Toast.makeText(MainActivity.this, "你點(diǎn)擊了Text_Button區(qū)間", Toast.LENGTH_SHORT).show(); break; } } }
展示效果:
三、注意細(xì)節(jié)
1.默認(rèn)狀態(tài)放在selector的最后
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@mipmap/b_qq" /> <item android:drawable="@mipmap/b_qq_pressed" android:state_pressed="true" /> </selector>
不能實(shí)現(xiàn)對(duì)應(yīng)效果?。。?/p>
2.TextView selector需要放置在 res/corlor目錄下
3.Button的點(diǎn)擊事件優(yōu)先級(jí)高于包含他的父控件,需要將他只為不可點(diǎn)擊狀態(tài),才能保證狀態(tài)的一致性。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
標(biāo)題名稱:Androidselector狀態(tài)選擇器的使用詳解
當(dāng)前地址:http://www.rwnh.cn/article6/jsdsig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、云服務(wù)器、靜態(tài)網(wǎng)站、營銷型網(wǎng)站建設(shè)、微信小程序、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)