<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 本節引言: > 本節給大家介紹的Android基本UI控件是:開關按鈕ToggleButton和開關Switch,可能大家對著兩個組件 并不熟悉,突然想起筆者的第一間外包公司,是否在wifi下聯網的開關,竟然用的TextView,然后叫美工 且兩個切換前后的圖,然后代碼中進行設置,當然點擊TextView的時候判斷狀態,然后設置對應的背景... > > 好吧,也是醉了,好吧...本節講解的兩個其實都是開關組件,只是后者需要在Android 4.0以后才能使用 所以AndroidManifest.xml文件中的minsdk需要 >= 14 否則會報錯~,先來看看這兩個控件長什么樣先, Android 5.0后這兩個控件相比以前來說好看了許多,先看下5.0前的樣子: > > **5.0以前的ToggleButton和Switch:**![](https://box.kancloud.cn/2015-12-01_565da625a2489.jpg)![](https://box.kancloud.cn/2015-12-01_565da625b181a.jpg)?**5.0版本:**![](https://box.kancloud.cn/2015-12-01_565da625f2d54.jpg) > > 好吧,鮮明的對比...接下來我們就來學習者兩個控件的使用吧,其實兩個的使用幾乎是相同的。 > > 開始之前貼下官方API先:[Switch](http://androiddoc.qiniudn.com/reference/android/widget/Switch.html);[ToggleButton](http://androiddoc.qiniudn.com/reference/android/widget/ToggleButton.html) * * * ## 1.核心屬性講解: ### 1)ToggleButton(開關按鈕) 可供我們設置的屬性: > * **android:disabledAlpha**:設置按鈕在禁用時的透明度 > * **android:textOff:**按鈕沒有被選中時顯示的文字 > * **android:textOn:**按鈕被選中時顯示的文字 另外,除了這個我們還可以自己寫個selector,然后設置下Background屬性即可~ ### 2) 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:**設置字體,默認支持這三種:sans, serif, monospace;除此以外還可以使用 其他字體文件(***.ttf**),首先要將字體文件保存在assets/fonts/目錄下,不過需要在Java代碼中設置:?**Typeface typeFace =Typeface.createFromAsset(getAssets(),"fonts/HandmadeTypewriter.ttf"); textView.setTypeface(typeFace);** * * * ## 2.使用示例: 因為比較簡單,所以我們把他們寫到一起,另外,我們為Switch設置下滑塊和底部的圖片,實現 一個類似于IOS 7的滑塊的效果,但是有個缺點就是不能在XML中對滑塊和底部的大小進行設置, 就是素材多大,Switch就會多大,我們可以在Java中獲得Drawable對象,然后對大小進行修改, 簡單的例子: **運行效果圖:** ![](https://box.kancloud.cn/2015-12-01_565da62617cfc.jpg) **實現代碼:**?先是兩個drawable的文件:?**thumb_selctor.xml:** ~~~ <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/switch_btn_pressed"/> <item android:state_pressed="false" android:drawable="@drawable/switch_btn_normal"/> </selector> ~~~ **track_selctor.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/switch_btn_bg_green"/> <item android:state_checked="false" android:drawable="@drawable/switch_btn_bg_white"/> </selector> ~~~ **布局文件:activity_main.xml:** ~~~ <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:orientation="vertical" tools:context=".MainActivity"> <ToggleButton android:id="@+id/tbtn_open" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:textOff="關閉聲音" android:textOn="打開聲音" /> <Switch android:id="@+id/swh_status" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="" android:textOn="" android:thumb="@drawable/thumb_selctor" android:track="@drawable/track_selctor" /> </LinearLayout> ~~~ **MainActivity.java:** ~~~ public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{ private ToggleButton tbtn_open; private Switch swh_status; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tbtn_open = (ToggleButton) findViewById(R.id.tbtn_open); swh_status = (Switch) findViewById(R.id.swh_status); tbtn_open.setOnCheckedChangeListener(this); swh_status.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { switch (compoundButton.getId()){ case R.id.tbtn_open: if(compoundButton.isChecked()) Toast.makeText(this,"打開聲音",Toast.LENGTH_SHORT).show(); else Toast.makeText(this,"打開聲音",Toast.LENGTH_SHORT).show(); break; case R.id.swh_status: if(compoundButton.isChecked()) Toast.makeText(this,"開關:ON",Toast.LENGTH_SHORT).show(); else Toast.makeText(this,"開關:OFF",Toast.LENGTH_SHORT).show(); break; } } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看