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

Android UI控件Switch的使用方法

 更新時(shí)間:2017年09月29日 14:06:00   作者:Vermouth_alone  
這篇文章主要為大家詳細(xì)介紹了Android UI控件Switch的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在Android中偶爾會(huì)用到開關(guān),Switch就是一個(gè)簡(jiǎn)單易使用的不錯(cuò)的控件。

首先,在布局中添加上Switch控件:

<Switch
    android:id="@+id/s_v"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:switchMinWidth="20dp"
    android:textOn="on"
    android:textOff="off"
    android:thumb="@drawable/thumb"
    android:track="@drawable/track" />

以下是該控件的常用屬性:

textOn:控件打開時(shí)顯示的文字
textOff:控件關(guān)閉時(shí)顯示的文字
thumb:控件開關(guān)的圖片
track:控件開關(guān)的軌跡圖片
typeface:設(shè)置字體類型
switchMinWidth:開關(guān)最小寬度
switchPadding:設(shè)置開關(guān) 與文字的空白距離
switchTextAppearance:設(shè)置文本的風(fēng)格
checked:設(shè)置初始選中狀態(tài)
splitTrack:是否設(shè)置一個(gè)間隙,讓滑塊與底部圖片分隔(API 21及以上)
showText:設(shè)置是否顯示開關(guān)上的文字(API 21及以上)

我們一般不會(huì)用該控件原本的樣式,那么我們就需要自己修改樣式了:

gray_thumb.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle" >

  <!-- 高度40 -->
  <size android:height="40dp" android:width="40dp"/>
  <!-- 圓角弧度 20 -->
  <corners android:radius="20dp"/>

  <!-- 變化率 -->
  <gradient
    android:endColor="#ffffff"
    android:startColor="#ffffff" />

  <stroke android:width="1dp"
    android:color="#9e9e9e"/>

</shape>

green_thumb.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle" >

  <!-- 高度40 -->
  <size android:height="40dp" android:width="40dp"/>
  <!-- 圓角弧度 20 -->
  <corners android:radius="20dp"/>

  <!-- 變化率 -->
  <gradient
    android:endColor="#ffffff"
    android:startColor="#ffffff" />

  <stroke android:width="1dp"
    android:color="#33da33"/>

</shape>

gray_track.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle" >

  <!-- 高度  此處設(shè)置寬度無(wú)效-->
  <size android:height="20dp"/>
  <!-- 圓角弧度 15 -->
  <corners android:radius="25dp"/>

  <!-- 變化率 定義從左到右的顏色不變 -->
  <gradient
    android:endColor="#9e9e9e"
    android:startColor="#9e9e9e" />

</shape>

green_track.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

  <!-- 高度40 -->
  <size android:height="20dp"/>
  <!-- 圓角弧度 20 -->
  <corners android:radius="25dp"/>

  <!-- 變化率 -->
  <gradient
    android:endColor="#33da33"
    android:startColor="#33da33" />

</shape>

thumb.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- 設(shè)置按鈕在不同狀態(tài)下的時(shí)候,按鈕不同的顏色 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

  <item android:state_checked="true" android:drawable="@drawable/green_thumb" />
  <item android:drawable="@drawable/gray_thumb" />

</selector>

track.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- 控制Switch在不同狀態(tài)下,底下下滑條的顏色 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

  <item android:state_checked="true" android:drawable="@drawable/green_track" />
  <item android:drawable="@drawable/gray_track" />

</selector>

在styles.xml中添加如下style:

<style name="s_true" parent="@android:style/TextAppearance.Small">
  <item name="android:textColor">#33da33</item>
</style>

<style name="s_false" parent="@android:style/TextAppearance.Small">
  <item name="android:textColor">#9b9b9b</item>
</style>

最后,只需要將控件實(shí)例化出來(lái)進(jìn)行相應(yīng)操作就可以了:

MainActivity.class:

public class MainActivity extends Activity{

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Switch aSwitch = (Switch) findViewById(R.id.s_v);
    aSwitch.setChecked(false);
    aSwitch.setSwitchTextAppearance(MainActivity.this,R.style.x1);
    aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        //控制開關(guān)字體顏色
        if (b) {
          aSwitch.setSwitchTextAppearance(MainActivity.this,R.style.s_true);
        }else {
          aSwitch.setSwitchTextAppearance(MainActivity.this,R.style.x1);
        }
      }
    });
  }
}

最終效果如下圖:

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

相關(guān)文章

最新評(píng)論

响水县| 和龙市| 沁水县| 衡阳市| 来安县| 大英县| 博兴县| 乌拉特前旗| 盐山县| 台前县| 南宫市| 会理县| 夏津县| 临洮县| 河源市| 拉孜县| 托克托县| 龙州县| 从化市| 潢川县| 田林县| 白银市| 厦门市| 乌兰县| 尖扎县| 康乐县| 成武县| 洛南县| 皋兰县| 锡林浩特市| 楚雄市| 交城县| 屏边| 澄城县| 增城市| 新昌县| 九龙城区| 马边| 色达县| 锡林郭勒盟| 民丰县|