這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)如何在Android UI中使用Switch控件,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的南岳網(wǎng)站設(shè)計(jì)、移動媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
首先,在布局中添加上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>
最后,只需要將控件實(shí)例化出來進(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); } } }); } }
上述就是小編為大家分享的如何在Android UI中使用Switch控件了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前名稱:如何在AndroidUI中使用Switch控件
文章URL:http://bm7419.com/article14/gochge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、外貿(mào)網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、網(wǎng)站營銷、ChatGPT、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)