以编程方式在 RadioButton 上设置的边距未应用
我试图为以编程方式添加到 RadioGroup 的 RadioButtons 设置边距,但失败了。:RadioButtons 已正确添加,但它们的边距为 0...
有人可以帮忙吗?
布局
<RadioGroup android:id="@+id/rg_nav" android:orientation="vertical"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</RadioGroup>
活动
float density = getResources().getDisplayMetrics().density;
rg_nav = (RadioGroup) findViewById(R.id.rg_nav);
LinearLayout.LayoutParams params_rb = new LinearLayout.LayoutParams(
(int)(8*density),
(int)(8*density));
int margin = (int)(6*density);
params_rb.setMargins(margin, margin, margin, margin);
for(String url : product.list_url_pic){
RadioButton radio_btn = new RadioButton(ProductHome.this);
radio_btn.setButtonDrawable(R.drawable.rb_nav);
radio_btn.setId(rb_id++);
rg_nav.addView(radio_btn, params_rb);
}
I'm trying to set a margin to RadioButtons added programmatically to a RadioGroup, but it fails.: the RadioButtons are correctly added, but they have 0 margin...
Anybody can help?
layout
<RadioGroup android:id="@+id/rg_nav" android:orientation="vertical"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</RadioGroup>
activity
float density = getResources().getDisplayMetrics().density;
rg_nav = (RadioGroup) findViewById(R.id.rg_nav);
LinearLayout.LayoutParams params_rb = new LinearLayout.LayoutParams(
(int)(8*density),
(int)(8*density));
int margin = (int)(6*density);
params_rb.setMargins(margin, margin, margin, margin);
for(String url : product.list_url_pic){
RadioButton radio_btn = new RadioButton(ProductHome.this);
radio_btn.setButtonDrawable(R.drawable.rb_nav);
radio_btn.setId(rb_id++);
rg_nav.addView(radio_btn, params_rb);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不得不替换
为
I had to replace
by
试试这个:
Try this:
您的代码似乎是正确的。
尝试将
LayoutParams
直接应用于您的按钮Your code seems to be correct.
Try applying
LayoutParams
directly to your buttons