AndroidUI之開關效果的設計

來源:互聯網
上載者:User

最終效果如下:  具體來說就是實現了checkbox自訂選中和為選擇樣式,功能表項目根據不同位置設定不同背景.先上整體布局檔案代碼:[html]  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"       android:background="#DFE1E0"     android:orientation="vertical">      <LinearLayout        style="@style/SettingItemTop"          android:background="@drawable/setting_list_top"          xmlns:android="http://schemas.android.com/apk/res/android"          xmlns:tools="http://schemas.android.com/tools"          android:layout_width="match_parent"          android:layout_height="wrap_content"           android:orientation="horizontal">           <TextView              style="@style/MySettingText"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:text="開啟通知"            />          <CheckBox               style="@style/MyCheckBox"              android:layout_width="wrap_content"              android:layout_height="wrap_content"/>      </LinearLayout>          <LinearLayout          style="@style/SettingItemMiddle"          android:layout_width="match_parent"          android:layout_height="wrap_content"           android:orientation="horizontal">          <TextView              style="@style/MySettingText"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:text="開啟鈴聲"            />          <CheckBox              style="@style/MyCheckBox"              android:layout_width="wrap_content"              android:layout_height="wrap_content"/>      </LinearLayout>          <LinearLayout          style="@style/SettingItemBottom"          android:layout_width="match_parent"          android:layout_height="wrap_content"           android:orientation="horizontal">           <TextView              style="@style/MySettingText"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:text="開啟震動"            />          <CheckBox               style="@style/MyCheckBox"              android:layout_width="wrap_content"              android:layout_height="wrap_content"/>      </LinearLayout>  </LinearLayout>  注意到:style="@style/MySettingText"這一句,這裡用了style.xml這個檔案來控制樣式.Android實際開發中,會有很多時候一些組件的屬性是要重複出現的,如果每個控制項都要單獨的 把這些屬性單獨的輸入一次,那樣會很沒有效率.可以將重複的代碼添加到style.xml中,設定成一種樣式,在用到這些屬性是引用這個樣式即 可.style="@style/MySettingText"這個是設定文字的屬性,代碼如下: [html]  <style name="MySettingText">          <item name="android:layout_margin">10dp</item>          <item name="android:layout_weight">6</item>  </style>  這裡僅用了兩個屬性.開發中可以根據實際需要自行修改.以後每個設定項的文字不需要再把這些屬性再添加一邊,引用一下這個style即可.CheckBox樣式自訂用到了selector.style="@style/MyCheckBox"此句對應的代碼: [html]  <style name="MyCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox">          <item name="android:button">@drawable/check</item>          <item name="android:layout_margin">10dp</item>          <item name="android:layout_weight">1</item>   </style>  注意到此句:<item name="android:button">@drawable/check</item>這裡就用了selector自訂樣式.在res/drawable下建立一名為check(自定)的xml檔案,代碼如下: [html] <?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/on"/>    <item android:state_checked="false" android:drawable="@drawable/off"/>  </selector>    其中@drawable/on為選擇時checkbox的映像,@drawable/off反之. 還注意到菜單中第一項上方為圓角,中間項為四角都為方,最後一項為下部圓角,這個就是通過設定不同的背景圖片實現的了,具體不細表.  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.