国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android UI控件Switch的使用方法

2019-10-22 18:26:51
字體:
供稿:網(wǎng)友

在Android中偶爾會用到開關(guān),Switch就是一個簡單易使用的不錯的控件。

首先,在布局中添加上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:控件打開時顯示的文字
textOff:控件關(guān)閉時顯示的文字
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è)置一個間隙,讓滑塊與底部圖片分隔(API 21及以上)
showText:設(shè)置是否顯示開關(guān)上的文字(API 21及以上)

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

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è)置寬度無效-->  <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)下的時候,按鈕不同的顏色 --><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>

最后,只需要將控件實例化出來進行相應(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);        }      }    });  }}

最終效果如下圖:

Android,UI,Switch

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


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 如东县| 乐昌市| 阿合奇县| 合阳县| 象州县| 祁东县| 驻马店市| 宜黄县| 城口县| 华安县| 梅河口市| 永嘉县| 山阴县| 景德镇市| 固始县| 临汾市| 万全县| 稻城县| 宣威市| 平泉县| 禄丰县| 定日县| 赣州市| 邢台市| 常山县| 称多县| 林西县| 乌拉特后旗| 塔城市| 兰溪市| 寿光市| 宜春市| 枣庄市| 太谷县| 西盟| 西乡县| 都昌县| 南召县| 涞水县| 廊坊市| 英山县|