项目迁移到androidx,用低版本的手机(android4.4)测试,发现xml中的用"android:button=null"不起作用了,所有的RadioButton和Checkbox自带的按钮都显示出来了。
查看源码,发现第二个构造参数多了个默认属性
com.android.internal.R.attr.radioButtonStyle
如:RadioButton和AppCompatRadioButton源码(CheckBox和AppCompatCheckBox一样)
RadioButton:
public class RadioButton extends CompoundButton {
public RadioButton(Context context) {
this(context, null);
}
public RadioButton(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.radioButtonStyle);
}
public RadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public RadioButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
AppCompatRadioButton:
public class AppCompatRadioButton extends RadioButton implements TintableCompoundButton,
TintableBackgroundView {
private final AppCompatCompoundButtonHelper mCompoundButtonHelper;
private final AppCompatBackgroundHelper mBackgroundTintHelper;
private final AppCompatTextHelper mTextHelper;
public AppCompatRadioButton(Context context) {
this(context, null);
}
public AppCompatRadioButton(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.radioButtonStyle);
}
public AppCompatRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(TintContextWrapper.wrap(context), attrs, defStyleAttr);
mCompoundButtonHelper = new AppCompatCompoundButtonHelper(this);
mCompoundButtonHelper.loadFromAttributes(attrs, defStyleAttr);
mBackgroundTintHelper = new AppCompatBackgroundHelper(this);
mBackgroundTintHelper.loadFromAttributes(attrs, defStyleAttr);
mTextHelper = new AppCompatTextHelper(this);
mTextHelper.loadFromAttributes(attrs, defStyleAttr);
}
public class MyRadioButton extends AppCompatRadioButton {
public MyRadioButton(Context context, AttributeSet attrs) {
super(context, attrs,0);
}
}
然后布局文件使用自定义的RadioButton