我用
appMusicTab.setButtonDrawable(R.drawable.all);
设置RadioButton的按钮样式,
但是drawable总是在RadioGroup左边的algin,我怎样才能在Code中为'button'设置左边距?谢谢!
我试过
appMusicTab.setPadding(20, 0, 0, 0);
和
appMusicTab.setLeft(20);
但那些没有用。
这是我的代码:
RadioButton appMusicTab = new RadioButton(mContext);
appMusicTab.setText(tabInfo.name);
appMusicTab.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 25);
appMusicTab.setGravity(Gravity.CENTER);
appMusicTab.setId(tabInfo.id);
// appMusicTab.setButtonDrawable(android.R.color.transparent);
appMusicTab.setButtonDrawable(R.drawable.all);
appMusicTab.setBackgroundResource(R.drawable.app_tab_bg);
appMusicTab.setPadding(30, 0, 0, 0);
RadioGroup.LayoutParams lp =
new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.MATCH_PARENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
lp.bottomMargin =MASettings.MAIN_UI_APP_TAB_MARGIN_BOTTOM;
mAppsTab.addView(appMusicTab, lp); //mAppsTab is a RadioGroup
答案 0 :(得分:3)
我使用带有透明填充的图标来解决此问题。我检查了RadioButton源代码,发现按钮Drawable的左边填充在代码中设置为0:buttonDrawable.setBounds(0,y,buttonDrawable.getIntrinsicWidth(),y + height);
答案 1 :(得分:2)
为此你必须设置LayoutParams。
见下文:
qButt = new Button(this);
qButt.setPadding(0, 0, 0, 0);
TableRow.LayoutParams params = new TableRow.LayoutParams(BtnHeight,BtnHeight); // You can set any Layout params in Place of TableRow
YourParentLayout.addView(qButt,params); // Your parent layout may be any Layout in xml