最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android selector狀態(tài)選擇器的使用詳解

 更新時間:2018年09月24日 11:00:11   作者:幻視  
這篇文章主要為大家詳細(xì)介紹了Android selector狀態(tài)選擇器的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

一、目的效果

       越好的用戶體驗來源更直接更明顯的事件反饋。selector可以“預(yù)存”多種響應(yīng)的反饋,主要以下多種狀態(tài)有:

android:state_selected是選中
android:state_focused是獲得焦點
android:state_pressed是點擊
android:state_enabled是設(shè)置是否響應(yīng)事件,指所有事件

       設(shè)置不同狀態(tài)的表現(xiàn)形式,則會在不同場景下有不同狀態(tài)。如文字:被選中狀態(tài),未被選中狀態(tài)。

       selector的普通使用則是為對應(yīng)單個控件添加以selector為背景的資源,則能達(dá)到目的。聯(lián)合使用則是基本使用一種升級。在我們的導(dǎo)航欄中,常使用LinearLayout或者RelativeLayout包含一個ImageView和一個TextView。圖片用于直觀觀感,文字用于更清晰的描述。

      在一個整體菜單被選中時,需要圖片及文字都表現(xiàn)對應(yīng)的狀態(tài)。并為保證較大的事件響應(yīng)范圍,點擊事件常賦予包含圖片和文字的父控件。即:為LinearLayout設(shè)置點擊事件,ImageView、TextView表現(xiàn)對應(yīng)的狀態(tài)。

二、具體實現(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, "你點擊了QQ登錄區(qū)間", Toast.LENGTH_SHORT).show();
        break;
      case R.id.weixin_ll:
        Toast.makeText(MainActivity.this, "你點擊了WeChat登錄區(qū)間", Toast.LENGTH_SHORT).show();
        break;
      case R.id.text_button_ll:
        Toast.makeText(MainActivity.this, "你點擊了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>

 不能實現(xiàn)對應(yīng)效果?。?!

2.TextView selector需要放置在 res/corlor目錄下

3.Button的點擊事件優(yōu)先級高于包含他的父控件,需要將他只為不可點擊狀態(tài),才能保證狀態(tài)的一致性。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android個人手機通訊錄開發(fā)詳解

    Android個人手機通訊錄開發(fā)詳解

    在本篇文章里小編給大家分享了關(guān)于Android個人手機通訊錄開發(fā)的步驟和相關(guān)源碼,有需要的朋友們學(xué)習(xí)下。
    2019-02-02
  • Android onTouchEvent事件中onTouch方法返回值(介紹)

    Android onTouchEvent事件中onTouch方法返回值(介紹)

    下面小編就為大家?guī)硪黄狝ndroid onTouchEvent事件中onTouch方法返回值(介紹)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • 8種android 對話框(Dialog)使用方法詳解

    8種android 對話框(Dialog)使用方法詳解

    這篇文章主要介紹了8種android 對話框(Dialog)使用方法。感興趣的朋友可以參考一下
    2016-03-03
  • Android開發(fā)實現(xiàn)圖片圓角的方法

    Android開發(fā)實現(xiàn)圖片圓角的方法

    這篇文章主要介紹了Android開發(fā)實現(xiàn)圖片圓角的方法,涉及Android針對圖形圖像的相關(guān)操作技巧,需要的朋友可以參考下
    2016-10-10
  • Android中WebView控件支持地理位置定位方法

    Android中WebView控件支持地理位置定位方法

    今天小編就為大家分享一篇Android中WebView控件支持地理位置定位方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Android高仿QQ小紅點功能

    Android高仿QQ小紅點功能

    這篇文章主要介紹了Android高仿QQ小紅點功能的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Android中ScrollView嵌套GridView的解決辦法

    Android中ScrollView嵌套GridView的解決辦法

    前些日子在開發(fā)中用到了需要ScrollView嵌套GridView的情況,由于這兩款控件都自帶滾動條,當(dāng)他們碰到一起的時候便會出問題,即GridView會顯示不全。下面小編給大家分享下解決方案,需要的朋友可以參考下
    2017-04-04
  • android studio 3.0 升級 項目遇到的問題及更改思路(問題小結(jié))

    android studio 3.0 升級 項目遇到的問題及更改思路(問題小結(jié))

    Android Studio從3.0版本新增了許多功能,當(dāng)然首當(dāng)其沖就是從3.0版本新增了對 Kotlin 開發(fā)語言的支持,除此之外還有其他一些新功能。很多小伙伴在android studio 3.0 升級項目遇到很多問題,下面小編給大家分享一些問題小結(jié)及解決辦法,一起看看吧
    2017-11-11
  • android studio2.3如何編譯動態(tài)庫的過程詳解

    android studio2.3如何編譯動態(tài)庫的過程詳解

    這篇文章主要給大家介紹了關(guān)于android studio 2.3如何編譯動態(tài)庫的過程,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08
  • Android中設(shè)置RadioButton在文字右邊的方法實例

    Android中設(shè)置RadioButton在文字右邊的方法實例

    這篇文章主要介紹了Android中設(shè)置RadioButton在文字右邊的方法實例,本文直接給出XML配置實現(xiàn)代碼,需要的朋友可以參考下
    2015-04-04

最新評論

上虞市| 巨鹿县| 中江县| 吴桥县| 固镇县| 涪陵区| 海门市| 济南市| 韶山市| 云安县| 思茅市| 孟村| 基隆市| 千阳县| 白河县| 万源市| 福建省| 隆子县| 陆丰市| 延吉市| 台湾省| 嘉定区| 文昌市| 永嘉县| 绵竹市| 恩施市| 阳城县| 永寿县| 长顺县| 方山县| 蓝田县| 永年县| 凤城市| 瓦房店市| 玉林市| 陇南市| 连山| 望奎县| 新巴尔虎左旗| 平顺县| 太保市|