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

首頁 > 系統 > Android > 正文

Android UI控件Switch的使用方法

2019-12-12 01:57:50
字體:
來源:轉載
供稿:網友

在Android中偶爾會用到開關,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:控件關閉時顯示的文字
thumb:控件開關的圖片
track:控件開關的軌跡圖片
typeface:設置字體類型
switchMinWidth:開關最小寬度
switchPadding:設置開關 與文字的空白距離
switchTextAppearance:設置文本的風格
checked:設置初始選中狀態
splitTrack:是否設置一個間隙,讓滑塊與底部圖片分隔(API 21及以上)
showText:設置是否顯示開關上的文字(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" >  <!-- 高度  此處設置寬度無效-->  <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"?><!-- 設置按鈕在不同狀態下的時候,按鈕不同的顏色 --><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在不同狀態下,底下下滑條的顏色 --><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>

最后,只需要將控件實例化出來進行相應操作就可以了:

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) {        //控制開關字體顏色        if (b) {          aSwitch.setSwitchTextAppearance(MainActivity.this,R.style.s_true);        }else {          aSwitch.setSwitchTextAppearance(MainActivity.this,R.style.x1);        }      }    });  }}

最終效果如下圖:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吉首市| 乳源| 扶风县| 崇信县| 眉山市| 南开区| 兴文县| 伽师县| 浦江县| 万宁市| 新巴尔虎左旗| 旺苍县| 大悟县| 宝鸡市| 清徐县| 慈溪市| 乐平市| 蓬安县| 曲水县| 马尔康县| 中卫市| 革吉县| 吴桥县| 巧家县| 东安县| 梁河县| 南雄市| 通州区| 翼城县| 社旗县| 扶沟县| 名山县| 湛江市| 炉霍县| 岑巩县| 长葛市| 伊宁县| 海宁市| 大埔县| 玛纳斯县| 侯马市|