4

我用

appMusicTab.setButtonDrawable(R.drawable.all); 

设置 RadioButton 的按钮样式,

但是drawable总是在RadioGroup的左侧,如何在代码中为“按钮”设置左填充?谢谢!!

我试过了

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
4

2 回答 2

3

我使用带有一些透明填充的图标来解决这个问题。查了RadioButton源码,发现在代码中按钮Drawable的左内边距设置为0:buttonDrawable.setBounds(0, y, buttonDrawable.getIntrinsicWidth(), y + height);

于 2012-09-12T09:27:52.377 回答
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
于 2012-09-05T09:08:45.607 回答