2016-07-15 75 views
6

我正在開發自定義複選框和單選按鈕,但樣式不適用於前棒棒糖設備(反而顯示黑色)。我已經編寫這樣的:風格不適用於自定義複選框和單選按鈕

XML:

<com.kaho.myapp.CustomCheckBox 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="CheckBoxText" 
    android:textColor="@color/colorPrimary" 
    android:theme="@style/SampleTheme"/> 

自定義複選框:

public class CustomCheckBox extends CheckBox { 
    public CustomCheckBox(Context context) { 
     super(context); 
    } 

    public CustomCheckBox(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setFont(context, attrs) ; 
    } 

    public CustomCheckBox(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     setFont(context,attrs) ; 
    } 

    public CustomCheckBox(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
     setFont(context, attrs) ; 
    } 

    private void setFont(Context context, AttributeSet attrs) { 
     if (attrs != null) { 
      /* Set the font */ 
     } 
    } 
} 

字體設置中正確。 風格:

<style name="SampleTheme" parent="Theme.AppCompat.Light"> 
    <item name="colorAccent">#08c283</item> 
    <item name="android:textColorSecondary">#969696</item> 
</style> 

回答

3

因爲預棒棒糖設備不具備你有這個問題默認設置爲colorAccent的可能性。要獲得這樣的行爲,請從相應的支持視圖中擴展視圖。會有這樣的事情:

public class CustomCheckBox extends AppCompatCheckBox 
public class CustomRadioButton extends AppCompatRadioButton 

這樣,您的意見將有預棒棒糖設備用材料的設計風格。

+0

謝謝你的工作...... – Nikesh

0

看看自定義複選框爲所有版本的前/後棒棒糖的偉大工程。

CustomCheckBox.java:

public class CustomCheckBox extends CheckedTextView { 
    private Drawable btnDrawable; 

    public CustomCheckBox(Context context) { 
     this(context, null); 
    } 

    public CustomCheckBox(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public CustomCheckBox(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     colorAccordingToTheme(); 
    } 

    @Override 
    public void setCheckMarkDrawable(Drawable d) { 
     super.setCheckMarkDrawable(d); 
     btnDrawable = d; 
    } 

    @Override 
    public void toggle() { 
     super.toggle(); 
     colorAccordingToTheme(); 
    } 

    @Override 
    public void setChecked(boolean checked) { 
     super.setChecked(checked); 
     colorAccordingToTheme(); 
    } 

    private void colorAccordingToTheme() { 
     if (btnDrawable != null) { 
      btnDrawable.setColorFilter(yourColor, PorterDuff.Mode.SRC_IN); 
     } 
    } 
} 

在XML佈局:

<?xml version="1.0" encoding="utf-8"?> 
<com.yourpaackcge.CustomCheckBox xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/cb" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:checkMark="@drawable/selector_check_box" 
    android:gravity="center|left" 
    android:paddingRight="24dp" 
    android:paddingLeft="24dp" 
    android:background="@drawable/ripple"/> 

我的選擇:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/ic_check_box_on" android:state_pressed="true"/> 

    <item android:drawable="@drawable/ic_check_box_on" android:state_checked="true"/> 

    <item android:drawable="@drawable/ic_check_box_off"/> 
</selector>