Android開關:Switch控制項

來源:互聯網
上載者:User

標籤:height   技術   http   inf   ccf   androi   引用   medium   family   

 

什麼是開關?先看一下Switch的實現效果

和手機上開啟移動資料的按鈕很像

 

 

屬性:

 

  • android:showText:設定on/off的時候是否顯示文字,boolean
  • android:splitTrack:是否設定一個間隙,讓滑塊與底部圖片分隔,boolean
  • android:switchMinWidth:設定開關的最小寬度
  • android:switchPadding:設定滑塊內文字的間隔
  • android:switchTextAppearance:設定開關的文字外觀,暫時沒發現有什麼用...
  • android:textOff:按鈕沒有被選中時顯示的文字
  • android:textOn:按鈕被選中時顯示的文字
  • android:textStyle:文字風格,粗體,斜體寫劃線那些
  • android:track:底部的圖片
  • android:thumb:滑塊的圖片
  • android:typeface:設定字型

 

使用這個控制項比較簡單,只要在布局檔案中引用,然後設定相關的屬性就可以了

 

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center"    tools:context="com.contentprovide.liuliu.togglebutton_test.MainActivity">   <Switch       android:id="@+id/swh"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:track="@drawable/tra"       android:thumb="@drawable/a3"       android:textOff="off"       android:textOn="on"       /></LinearLayout>

 

 

 

 

我這裡的track和thumb引用的兩個drawable資源檔是我自己定義的

 

tra.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/a2"></item><item android:state_checked="false" android:drawable="@drawable/a1"></item></selector>

 

 

a1.xml、a2.xml、a3.xml是自訂圖形,比較簡單,這裡就不記錄了

 

Android開關:Switch控制項

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.