【问题标题】:Style is not applying in custom checkbox and radiobutton样式不适用于自定义复选框和单选按钮
【发布时间】:2016-07-15 08:58:53
【问题描述】:

我正在开发自定义复选框和单选按钮,但样式不适用于棒棒糖前设备(改为显示黑色)。我是这样编码的:

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>

【问题讨论】:

    标签: android android-theme android-styles


    【解决方案1】:

    您遇到此问题是因为棒棒糖之前的设备默认无法设置colorAccent。要获得这样的行为,请从相应的支持视图扩展您的视图。会有这样的事情:

    public class CustomCheckBox extends AppCompatCheckBox
    public class CustomRadioButton extends AppCompatRadioButton
    

    这样,您的视图将具有之前 Lollipop 设备上的材料设计风格。

    【讨论】:

      【解决方案2】:

      看看自定义复选框,它适用于棒棒糖前后的所有版本。

      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>
      

      【讨论】:

        猜你喜欢
        • 2017-01-07
        • 2015-12-09
        • 2021-10-06
        • 2012-01-07
        • 2015-10-04
        • 2012-01-22
        • 2023-04-09
        • 2016-05-13
        相关资源
        最近更新 更多