2015-04-02 144 views
2

我試圖從AppCompat實現SwitchCompat,但它在不同版本的設備上看起來不同。 在棒棒糖& Froyo它看起來不錯,但薑餅對KitKat它看起來不像開關。SwitchCompat上棒棒糖設備

代碼:

<android.support.v7.widget.SwitchCompat 
     android:id="@+id/label_switch" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textOff="No" 
     android:textOn="Yes" 
     android:checked="false" /> 

我可以讓這些交換機看起來是一樣的所有版本,或者至少使它們看起來像一個開關?

+0

的[Switchcompat不顯示開關]可能重複(http://stackoverflow.com/問題/ 26563986/switchcompat-not-displays-the-switch) – 2015-04-02 17:17:31

+0

接受的答案是使用'Theme.AppCompat'作爲父項,但我僅使用'Theme.AppCompat.NoActionBar'(使用工具欄)。 – user3677365 2015-04-02 17:28:17

+0

如何將9patch和其他文件複製到項目中並使其工作?只有在您的項目沒有任何問題時纔會這樣做。 – 2015-04-03 12:50:41

回答

9

我的應用程序的Min sdk是GingerBread,而且我遇到了同樣的問題,最後我找到了解決方案。爲了使SwitchCompat在所有Android版本中保持一致,我使用了兩個可繪製文件夾res/drawable,其中一個用於thumb,另一個用於track。並將它們分配給不在xml中的java代碼中的SwitchCompat。這是您應該使用的代碼。

SwitchCopmat插件:

<android.support.v7.widget.SwitchCompat 
android:id="@+id/label_switch" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"/> 

可繪製爲拇指,switch_compat_thumb.xml

<?xml version="1.0" encoding="utf-8"?> 
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<item 
    android:bottom="@dimen/switch_compat_thumb_margin" 
    android:left="@dimen/switch_compat_thumb_margin" 
    android:right="@dimen/switch_compat_thumb_margin" 
    android:top="@dimen/switch_compat_thumb_margin"> 

    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
     <item android:state_checked="true"> 
      <shape android:shape="oval"> 
       <size 
        android:width="@dimen/switch_compat_thumb_size" 
        android:height="@dimen/switch_compat_thumb_size"/> 
       <solid android:color="@android:color/red"/> 
      </shape> 
     </item> 

     <item> 
      <shape android:shape="oval"> 
       <size 
        android:width="@dimen/switch_compat_thumb_size" 
        android:height="@dimen/switch_compat_thumb_size"/> 
       <stroke 
        android:width="@dimen/switch_compat_thumb_stroke_width" 
        android:color="@android:color/red"/> 
       <solid android:color="@android:color/transparent" /> 
      </shape> 
     </item> 
    </selector> 
</item> 

可繪製爲track,switch_compat_track.xml

<?xml version="1.0" encoding="utf-8"?> 
    <shape 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 
<corners android:radius="@dimen/switch_compat_track_radius"/> 
<stroke 
    android:width="@dimen/switch_compat_track_stroke_width" 
    android:color="@android:color/red"/> 
<solid android:color="@android:color/transparent" /> 

,然後,在Java發現它後,在java代碼分配thumbtrackSwitchCompat

final SwitchCopmat switchCompat = (SwitchCopmat) findViewById(R.id.label_switch); 

    //add thumb and track drawable in java since it doesn't work on xml for gingerbread 
    switchCompat.setThumbDrawable(getResources().getDrawable(R.drawable.switch_compat_thumb)); 
    switchCompat.setTrackDrawable(getResources().getDrawable(R.drawable.switch_compat_track)); 
+0

這幾乎適用於我,但是你能否包含維度的定義? Margina,寬度等。謝謝 – shtolik 2015-09-23 10:49:48

+4

@shtolik在switch_compat_thumb.xml中,項目(button,left,top,right)有4dp,那麼第一個橢圓形狀有8dp(寬度和高度),strock有4dp寬度。第二個橢圓形狀的寬度和高度再次爲8dp,行程寬度爲2dp。而在switch_compat_track.xml中,矩形形狀具有半徑爲7dp的拐角和以2dp寬度劃分的拐角 – Sherry 2015-09-24 18:21:28

相關問題