【发布时间】:2016-10-19 08:18:37
【问题描述】:
我制作了一个自定义 RadioButton,在 Android 5.0 设备中如下所示。
这些 RadioButtons 是动态创建的,如以下方法所示。所以第一个方法 redioButtonPresenterApparence 设置它的外观去除圆圈(设置 buttonDrwable 为 null。第二个方法稍后设置按钮背景。
private void radioButtonPresenterApparence(RadioButton presenter, int icon) {
Drawable drawable = getResources().getDrawable(icon);
presenter.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
presenter.setButtonDrawable(null);
presenter.setGravity(Gravity.CENTER);
}
private void updateButtonsBackground(){
int childCount = getChildCount();
BackgroundSelector bgSelector = new BackgroundSelector(childCount);
for(int i = 0; i < childCount; i++){
View rb = getChildAt(i);
rb.setBackgroundResource( bgSelector.getBackgroundResource(i) );
rb.requestLayout();
}
requestLayout();
}
我的问题是在三星 Android 4.4.4 设备上进行相同测试时(不确定其他制造商),它显示如下。
PS:这是一个代码创建的 RadioButton。您可以通过以下方法查看:
private void addPresenter(int icon){
RadioButton presenter = new RadioButton(getContext()); //Create new RadioButton
radioButtonPresenterApparence(presenter, icon); //Set icon and configure aparence
addView(presenter); //Add new button to Selector
presenterParentAparance(presenter); //Config button inside parent
requestLayout(); //Request layout update to Selector
}
【问题讨论】:
-
Google 搜索“android 自定义单选按钮”
-
尝试将 android:button="@null" 放入您的 xml 并运行
-
@AnuragPandit 我已经在
presenter.setButtonDrawable(null)做这件事了