android以编程方式添加放射组按钮之间的填充

前端之家收集整理的这篇文章主要介绍了android以编程方式添加放射组按钮之间的填充前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在xml中有一个radiogroup,按钮是以编程方式生成的.如何以编程方式添加按钮之间的间距.

我以为是像LayoutParams这样的东西,但是我的对象并没有一个明显的setPadding或setMargins方法.

这是我正在尝试的

@H_502_6@RadioButton currentButton = new RadioButton(context); currentButton.setText(item.getLabel()); currentButton.setTextColor(Color.BLACK); //add padding between buttons LayoutParams params = new LayoutParams(context,null); params. ... ?????? currentButton.setLayoutParams(params);

解决方法

填充

普通LayoutParams没有应用填充的方法,但是视图.由于RadioButton是一个子视图,您可以使用View.setPadding(),例如:

@H_502_6@currentButton.setPadding(0,10,10);

这在顶部增加了10px的填充,底部增加了10px.如果要在px旁边使用其他单位(例如dp),则可以先将TypedValue.applyDimension()转换为像素.

边距

边距应用于某些特定的LayoutParams类,它们是MarginLayoutParams子类.确保在设置边距时使用特定的子类. RadioGroup.LayoutParams而不是通用的ViewGroup.LayoutParams(当您的父版本是RadioGroup时).那么你可以简单地使用MarginLayoutParams.setMargins().

样品:

@H_502_6@RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(context,null); params.setMargins(10,0); currentButton.setLayoutParams(params);

猜你在找的Android相关文章