以编程方式添加的 RadioButtons 拒绝服从 LayoutParams 权重 [英] Programmatically-added RadioButtons refuse to obey LayoutParams weighting

查看:15
本文介绍了以编程方式添加的 RadioButtons 拒绝服从 LayoutParams 权重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Android 布局中创建一个 RadioGroup,其中子 RadioButton 被拉伸以均匀地填充 RadioGroup.但是,当我尝试使用从代码中以编程方式添加的 RadioButton 执行此操作时遇到了一些意外行为.首先是一些背景...

I'm trying to create a RadioGroup within an Android layout where the child RadioButtons are stretched to evenly fill the entire width of the RadioGroup. However, I've encountered some unexpected behaviour when trying to do this with RadioButtons which have been added programmatically from code. First some background...

我从一个基于 RelativeLayout 的简单布局开始,它在底部包含一个大的 TextView 和一个 RadioGroup.

I started with a simple layout based on a RelativeLayout which contains a large TextView and a RadioGroup at the bottom.

main.xml 布局文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView android:text="Some text"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/radio_group"
        android:gravity="center"
        android:background="@android:color/holo_green_dark"
        />      
    <RadioGroup android:id="@+id/radio_group"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:background="@android:color/holo_blue_dark">         
        <RadioButton android:text="Option 1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/darker_gray"
            android:button="@android:color/transparent"
            android:padding="10dp"
            android:gravity="center"
            android:layout_margin="2dp"/>   
        <RadioButton android:text="Option 2"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/darker_gray"
            android:button="@android:color/transparent"
            android:padding="10dp"
            android:gravity="center"
            android:layout_margin="2dp"/> 
    </RadioGroup>       
</RelativeLayout>

在运行时生成以下布局:

which produces the following layout at runtime:

可以看到 android:layout_width="wrap_content"android:layout_weight="1" 在两个 RadioButton 中的使用拉伸它们以均匀地填充每个封闭的 RadioGroup 的一半.到目前为止一切顺利.

You can see that the use of android:layout_width="wrap_content" and android:layout_weight="1" in both RadioButtons stretches them to evenly fill half of the enclosing RadioGroup each. So far so good.

但是,我的要求是在运行时根据业务逻辑在此布局中动态创建 RadioButton,而不是始终使用布局中静态包含的两个按钮 - 有时我可能需要两个按钮,有时是四个等等.

However, the requirement I have is to dynamically create RadioButtons within this layout at runtime based on business logic rather than always using the the two statically included in the layout - sometimes I might need two buttons, sometimes four etc.

为了实现这一点,我从我的 main.xml 布局中删除了 RadioButton:

To implement this I removed the RadioButtons from my main.xml layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView android:text="Some text"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/radio_group"
        android:gravity="center"
        android:background="@android:color/holo_green_dark"
        />      
    <RadioGroup android:id="@+id/radio_group"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:background="@android:color/holo_blue_dark"/>
</RelativeLayout>

...并为我的 RadioButton 创建了一个单独的 _radio_button.xml_ 布局:

...and created a separate _radio_button.xml_ layout for my RadioButton:

<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="2dp"
    android:layout_weight="1"
    android:background="@android:color/darker_gray"
    android:button="@android:color/transparent"
    android:gravity="center"
    android:padding="10dp" />

在我的活动中,我现在以编程方式添加 RadioButton:

In my activity I now add the RadioButtons programmatically:

public class TestRadioActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Create an inflater to inflate our buttons            
        LayoutInflater inflater = 
            (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // Create the layout params for our buttons
        LinearLayout.LayoutParams layoutParams = new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f);

        RadioGroup group = (RadioGroup) findViewById(R.id.radio_group);

        // Add button one
        RadioButton button = (RadioButton) inflater.inflate(R.layout.radio_button, null);
        button.setText("Option 1");
        group.addView(button, layoutParams);

        // Add button two
        button = (RadioButton) inflater.inflate(R.layout.radio_button, null);
        button.setText("Option 2");
        group.addView(button, layoutParams);

    }
}

注意 _radio_button.xml_ 文件和活动如何指定 WRAP_CONTENT 的布局宽度和 1 的布局权重,以便像原始 main.xml 一样均匀分布按钮.

Note how both the _radio_button.xml_ file and the activity specify a layout width of WRAP_CONTENT and a layout weight of 1 to evenly distribute the buttons as in the original main.xml.

然而,布局似乎被渲染而忽略了布局权重,按钮位于单选组左侧:

However, the layout seems to get rendered ignoring the layout weight with the buttons butted up on the left of the radio group:

正如其他地方所建议的,我还尝试在 LayoutParams 中将 RadioButtons 的宽度设置为 0(显然这会导致布局权重被稍微解释)不同),但这会导致 RadioButton 甚至不会被渲染:

As has been suggested elsewhere, I also tried setting the width of the RadioButtons to 0 in the LayoutParams (apparently this can cause the layout weight to be interpreted slightly differently), but this causes the RadioButtons not even to be rendered:

有什么建议可以在以编程方式添加时如何让 RadioButton 均匀地填充包含 RadioGroup 的整个宽度?有什么明显的我遗漏了吗?

Can any advise how to get RadioButtons to evenly fill the entire width of the containing RadioGroup when added programmatically? Is there anything obvious I'm missing?

推荐答案

设置布局权重时,应使用fill_parent 作为布局宽度.那么您不应该使用 LinearLayout.LayoutParams 而是 RadioGroup.LayoutParams,因为您将单选按钮添加到 RadioGroup,而不是简单的 LinearLayout.

When you set a layout weight, you should use fill_parent as layout width. Then you shouldn't use LinearLayout.LayoutParams but RadioGroup.LayoutParams, as you're adding radio buttons to a RadioGroup, not to a simple LinearLayout.

最后,当您使用充气器构建"单选按钮时,单选按钮的 XML 文件已经具有从 XML 文件中选取的布局参数,因此我认为您应该只调用 addView 方法,该方法只需要视图添加为参数(即 addView(View v))并将 layout_width 更改为 fill_parent.

Finally, as you use the inflater to "build" the radio button, the XML file of the radio button already has the layout params picked from the XML file, so I think you should just call the addView method that takes only the view to add as parameter (that is addView(View v)) and change the layout_width to fill_parent.

请注意,如果您需要在代码中引用变量button",即添加一个点击监听器,您将只将监听器添加到最后创建的按钮上.您必须为要添加到 RadioGroup(按钮、按钮 1、按钮 2 等)的每个 RadioButton 创建一个 RadioButton 对象.

Note that, if you'll need to reference the variable "button" in the code, i.e. add a click listener, you'll add the listener only to the last created button. You'll have to create a RadioButton object for each RadioButton you will add to the RadioGroup (button, button1, button2, etc).

这篇关于以编程方式添加的 RadioButtons 拒绝服从 LayoutParams 权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆