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

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

概述我在xml中有一个radiogroup,按钮是以编程方式生成的.如何以编程方式添加按钮之间的间距. 我以为是像LayoutParams这样的东西,但是我的对象并没有一个明显的setPadding或setMargins方法. 这是我正在尝试的 RadioButton currentButton = new RadioButton(context); currentButton 我在xml中有一个radiogroup,按钮是以编程方式生成的.如何以编程方式添加按钮之间的间距.

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

这是我正在尝试的

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(),例如:

currentbutton.setpadding(0,10,10);

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

边距

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

样品:

RadioGroup.LayoutParams params            = new RadioGroup.LayoutParams(context,null);params.setmargins(10,0);currentbutton.setLayoutParams(params);
总结

以上是内存溢出为你收集整理的android以编程方式添加放射组按钮之间的填充全部内容,希望文章能够帮你解决android以编程方式添加放射组按钮之间的填充所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1131670.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存