Android, How to programmatically set the left spacer of a Radiobutton button?

I use

appMusicTab.setButtonDrawable(R.drawable.all); 

to set the style of the RadioButton button,

But popped is always the algin to the left of RadioGroup, how can I set the left addition to the 'button' in the code? Thanks!!

I tried

 appMusicTab.setPadding(20, 0, 0, 0); 

and

  appMusicTab.setLeft(20); 

But that did not work.

This is my code:

  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
source share
2 answers

I used the icon with some transparent paddings to solve this problem. I checked the source code of RadioButton and found that the left padding of the Drawable button is set to 0: codeDrawable.setBounds (0, y, buttonDrawable.getIntrinsicWidth (), y + height);

+3
source

For this you need to install LayoutParams.

See below:

 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 

Hope this helps you. If not, let me know.

Enjoy the download :)

+2
source

Source: https://habr.com/ru/post/1432430/


All Articles