以编程方式添加的单选按钮不服的LayoutParams权单选、按钮、方式、LayoutParams

2023-09-05 06:47:47 作者:栀寒老酒

我试图创建一个 RadioGroup中中的一个Android的布局,其中的子单选 s的拉伸均匀填写 RadioGroup中的整个宽度。不过,我遇到试图用单选■哪些已添加编程方式从code做到这一点时,一些意外的行为。一是一些背景......

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 的布局文件看起来是这样的:

The main.xml layout file looks like this:

<?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在两个单选取值延伸他们均匀地填充半封闭 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.

不过,要求我必须是动态的创建单选此布局在运行时内S的基于业务逻辑,而不是总是使用两个静态包含在布局 - 有时我可能需要两个按钮,有时4等。

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 的布局:

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>

...并创建了一个单独_radio_button.xml_布局我的单选

<?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" />

在我的活动我现在添加单选取值编程方式:

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:

由于已建议在其他地方,我也试过单选 S的宽度设置为0,在的LayoutParams (很明显,这可能会导致布局重量要PTED略有不同跨$ P $),但是这会导致单选不是甚至被渲染:

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:

可以在任何告知如何获得单选 s要均匀地填充的整个宽度包含 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。最后,当您使用充气建设单选按钮,单选按钮已经有了布局PARAMS从XML文件中选择的xml文件,所以我觉得你应该只调用addView方法只需视图添加作为参数(即addView(视图V)),并改变layout_width到FILL_PARENT。需要注意的是,如果你需要引用在code变量按钮,即增加一个点击监听器,你将添加侦听器只到最后创建的按钮。你必须创建一个单选按钮对象的每个单选按钮,您将添加到RadioGroup中(按钮,按钮1,按钮2等)。

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. 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. 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).