以编程方式在 RadioButton 上设置的边距未应用

发布于 2024-12-12 04:58:44 字数 989 浏览 3 评论 0原文

我试图为以编程方式添加到 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?

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

手长情犹 2024-12-19 04:58:44

我不得不替换

LinearLayout.LayoutParams params_rb = new LinearLayout.LayoutParams(
    (int)(8*density),
    (int)(8*density));

RadioGroup.LayoutParams params_rb = new RadioGroup.LayoutParams(
                (int)(8*density),
                (int)(8*density));

I had to replace

LinearLayout.LayoutParams params_rb = new LinearLayout.LayoutParams(
    (int)(8*density),
    (int)(8*density));

by

RadioGroup.LayoutParams params_rb = new RadioGroup.LayoutParams(
                (int)(8*density),
                (int)(8*density));
苍暮颜 2024-12-19 04:58:44

试试这个:

TableRow.LayoutParams rg_params = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 3f);
RadioGroup radio_group=new RadioGroup(this);
radio_group.setOrientation(RadioGroup.HORIZONTAL);
radio_group.setLayoutParams(rg_params);

RadioButton dry=new RadioButton(this);
RadioGroup.LayoutParams params_soiled = new RadioGroup.LayoutParams(getBaseContext(), null);
params_soiled.setMargins(10, 0, 10, 0);
dry.setLayoutParams(button_params);

Try this:

TableRow.LayoutParams rg_params = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 3f);
RadioGroup radio_group=new RadioGroup(this);
radio_group.setOrientation(RadioGroup.HORIZONTAL);
radio_group.setLayoutParams(rg_params);

RadioButton dry=new RadioButton(this);
RadioGroup.LayoutParams params_soiled = new RadioGroup.LayoutParams(getBaseContext(), null);
params_soiled.setMargins(10, 0, 10, 0);
dry.setLayoutParams(button_params);
羞稚 2024-12-19 04:58:44

您的代码似乎是正确的。
尝试将 LayoutParams 直接应用于您的按钮

radio_btn.setButtonDrawable(R.drawable.rb_nav);
radio_btn.setId(rb_id++);    
rg_nav.addView(radio_btn);
radio_btn.setLayoutParams(params_rb);

Your code seems to be correct.
Try applying LayoutParams directly to your buttons

radio_btn.setButtonDrawable(R.drawable.rb_nav);
radio_btn.setId(rb_id++);    
rg_nav.addView(radio_btn);
radio_btn.setLayoutParams(params_rb);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文