public void addRadioGropItem(){
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int indicatorWidth = dm.widthPixels / arr.length;
mRadioGroup.removeAllViews();
for(int i=0;i<5;i++){
RadioButton rb = (RadioButton)this.getLayoutInflater().inflate(R.layout.activity_main_radiogroup_item, null);
rb.setId(i);
rb.setText(arr[i]);
Drawable drawable=getResources().getDrawable(R.drawable.btn_home);
int w = drawable.getIntrinsicWidth();
int h=drawable.getIntrinsicHeight();
/////这一步必须要做,否则不会显示.
drawable.setBounds(0, 0, w, h);
rb.setCompoundDrawables(null,drawable,null,null);
rb.setPadding(0,20, 0, 0);
、
//rb.setCompoundDrawablesWithIntrinsicBounds(null,drawable,null,null);
//rb.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.btn_home,0,0);
rb.setLayoutParams(new LayoutParams(indicatorWidth,LayoutParams.MATCH_PARENT));
mRadioGroup.addView(rb);
}
}
Radiobutton添加内容有三种方式:
1. Drawable drawable= getResources().getDrawable(R.drawable.btn_home);
int w = drawable.getIntrinsicWidth();
int h=drawable.getIntrinsicHeight();
/////这一步必须要做,否则不会显示.
drawable.setBounds(0, 0, w, h);
rb.setCompoundDrawables(null,drawable,null,null);
rb.setPadding(0,20, 0, 0);
2. rb.setCompoundDrawablesWithIntrinsicBounds(null,drawable,null,null);
3. rb.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.btn_home,0,0);