如何动态地添加单选按钮,而不是失去浏览内容 [英] How to dynamically add radiobuttons and not lose view contents

查看:126
本文介绍了如何动态地添加单选按钮,而不是失去浏览内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的布局看起来像这样

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:方向=垂直的android:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT>
        <的TextView的android:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文字样式=大胆
            机器人:文字=请选择要评级的餐厅:/>        < RadioGroup中的android:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:方向=垂直
            机器人:ID =@ + ID / SelectRestaurant>         < / RadioGroup中>
         <按钮的android:layout_width =100px的
         机器人:layout_height =WRAP_CONTENT
         机器人:文字=给它
         机器人:ID =@ + ID / submitSelection/>
< / LinearLayout中>

当我在Java文件中添加单选按钮到RadioGroup中,单选按钮重叠彼此并提交按钮不见了。我猜单选按钮被覆盖的视图等元素。我如何动态地添加这些单选按钮不失其他元素。

Java的code是这样的:

 的setContentView(R.layout.submit_select);
        RadioGroup中radioGroup中=(RadioGroup中)findViewById(R.id.SelectRestaurant);
        对于(字符串餐厅:餐厅){
            单选按钮单选按钮=新的单选按钮(getBaseContext());
            radioButton.setText(餐厅);
            radioGroup.addView(单选)
        }
        radioGroup.invalidate();
        dismissDialog(DIALOG_SUBMIT_ID);
        rateItButton =(按钮)findViewById(R.id.submitSelection);
        rateItButton.setOnClickListener(本);


解决方案

尝试用WRAP_CONTENT的宽度和高度创建布局参数,然后添加的LayoutParams当您添加的按钮组:

  radiogroup.addView(newRadioButton,的LayoutParams);

My layout looks like this

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:textStyle="bold" 
            android:text="Please select the restaurant you want to rate: " />

        <RadioGroup android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/SelectRestaurant">

         </RadioGroup>
         <Button android:layout_width="100px"
         android:layout_height="wrap_content" 
         android:text="Rate it" 
         android:id="@+id/submitSelection"/>
</LinearLayout>

When I add radiobuttons to the radiogroup in java file, radiobuttons are overlapping on each other and submit button is gone. I guess radiobuttons are overriding other elements of view. How can I dynamically add these radio buttons without losing other elements.

Java code looks like this:

setContentView(R.layout.submit_select);
        RadioGroup radioGroup = (RadioGroup)findViewById(R.id.SelectRestaurant);
        for (String restaurant : restaurants) {
            RadioButton radioButton = new RadioButton(getBaseContext());
            radioButton.setText(restaurant);
            radioGroup.addView(radioButton);
        }
        radioGroup.invalidate();
        dismissDialog(DIALOG_SUBMIT_ID);
        rateItButton = (Button) findViewById(R.id.submitSelection);
        rateItButton.setOnClickListener(this);

解决方案

Try creating Layout Parameters with WRAP_CONTENT for the width and height and then adding the layoutParams when you add the button to the group:

radiogroup.addView(newRadioButton, layoutParams);

这篇关于如何动态地添加单选按钮,而不是失去浏览内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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