Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 以编程方式添加的单选按钮拒绝遵守LayoutParams权重_Android_User Interface_Android Widget_Android Listview - Fatal编程技术网

Android 以编程方式添加的单选按钮拒绝遵守LayoutParams权重

Android 以编程方式添加的单选按钮拒绝遵守LayoutParams权重,android,user-interface,android-widget,android-listview,Android,User Interface,Android Widget,Android Listview,我试图在Android布局中创建一个RadioGroup,其中子RadioButton被拉伸,以均匀地填充RadioGroup的整个宽度。然而,我在尝试使用从代码中以编程方式添加的RadioButtons执行此操作时遇到了一些意外行为。首先是一些背景 什么有效 我从一个基于RelativeLayout的简单布局开始,它在底部包含一个大型TextView和一个RadioGroup main.xml布局文件如下所示: <?xml version="1.0" encoding="utf-8"?&

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

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

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>
…并为我的
RadioButton
创建了一个单独的_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" />
请注意_radio_button.xml文件和活动如何指定换行内容的布局宽度和布局权重1,以便像原始main.xml一样均匀分布按钮

但是,通过将按钮对接在单选组左侧,忽略布局权重来渲染布局:

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


当以编程方式添加时,有人能建议如何使
RadioButton
s均匀填充包含
射线组的整个宽度吗?有什么明显的遗漏吗?

设置布局权重时,应使用填充父项作为布局宽度。然后,在将单选按钮添加到射线组而不是简单的线性布局时,不应使用LinearLayout.LayoutParams,而应使用RadioGroup.LayoutParams

最后,当您使用充气器“构建”单选按钮时,单选按钮的XML文件已经具有从XML文件中选取的布局参数,因此我认为您应该调用addView方法,该方法只将要添加的视图作为参数(即
addView(view v)
),并更改布局宽度以填充父视图


注意,如果您需要在代码中引用变量“button”,即添加一个click侦听器,那么您将只将侦听器添加到最后创建的按钮。您必须为要添加到RadioButton组的每个RadioButton(button、button1、button2等)创建一个RadioButton对象。

FYI,这样做根本不需要任何xml

    RadioGroup rgrp = new RadioGroup(context);
    rgrp.setLayoutParams(new RadioGroup.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
    rgrp.setOrientation(LinearLayout.HORIZONTAL);

        mAccent = new RadioButton(context);
        mAccent.setText("Accent");
        mAccent.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
        rgrp.addView(mAccent);

        mGhost = new RadioButton(context);
        mGhost.setText( "Ghost");
        mGhost.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
        rgrp.addView(mGhost);

        mFlam = new RadioButton(context);
        mFlam.setText( "Flam");
        mFlam.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
        rgrp.addView(mFlam);

    layout.addView(rgrp);

我也遇到了这个问题,我使用了定义了权重的
RadioGroup.LayoutParams
。然而,我也发现,一旦我通过编程创建了按钮,按钮对触摸没有反应,所以将
可点击
启用
设置为
,这就解决了问题

  private RadioButton createMyTypeRadioButton(MyType type){

    //create using this constructor to use some of the style definitions
    RadioButton radio = new RadioButton(this, null, R.style.MyRadioStyle);

    RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
    radio.setLayoutParams(layoutParams);
    radio.setGravity(Gravity.CENTER);

    //tag used by the setOnCheckedChangeListener to link the radio button with mytype object
    radio.setTag(type.getId());

    //enforce enabled and clickable status otherwise they ignore clicks
    radio.setClickable(true);
    radio.setEnabled(true);

    radio.setText(type.getTitle());


    return radio;
}

private void updateMyTypesUi() {

    //populate RadioGroup with permitted my types
    for (int i = 0; i < myTypes.size(); i++) {
        MyType type = myTypes.get(i);           
        RadioButton radioButton = createSwapTypeRadioButton(type);
        myRadioGrp.addView(radioButton);

    }

    myRadioGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton checkedType = (RadioButton) group.findViewById(checkedId);              
            String idOfMyTypeChecked = (String) checkedType.getTag();
            //do something with idOfMyTypeChecked
        }
    });
}
private RadioButton创建MyTypeRadioButton(MyType类型){
//使用此构造函数创建以使用某些样式定义
RadioButton单选=新的RadioButton(this,null,R.style.MyRadioStyle);
RadioGroup.LayoutParams LayoutParams=新的RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_内容,ViewGroup.LayoutParams.WRAP_内容,1f);
radio.setLayoutParams(layoutParams);
无线电。设置重力(重力。中心);
//setOnCheckedChangeListener用于将单选按钮链接到mytype对象的标记
setTag(type.getId());
//强制启用和可单击状态,否则它们将忽略单击
收音机。可点击设置(真);
radio.setEnabled(真);
radio.setText(type.getTitle());
回程无线电;
}
私有void updateMyTypesUi(){
//使用允许的my类型填充RadioGroup
对于(int i=0;i
谢谢。更改为RadioGroup.LayoutParams修复了它-我没有意识到它是可用的。为什么我必须显式指定参数,而不是遵守我的xml参数?为什么这不像其他任何地方一样,需要我将
layout\u width
设置为
0dp
以使权重起作用?通过将父项与无线组的宽度进行匹配,效果很好。
    RadioGroup rgrp = new RadioGroup(context);
    rgrp.setLayoutParams(new RadioGroup.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
    rgrp.setOrientation(LinearLayout.HORIZONTAL);

        mAccent = new RadioButton(context);
        mAccent.setText("Accent");
        mAccent.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
        rgrp.addView(mAccent);

        mGhost = new RadioButton(context);
        mGhost.setText( "Ghost");
        mGhost.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
        rgrp.addView(mGhost);

        mFlam = new RadioButton(context);
        mFlam.setText( "Flam");
        mFlam.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
        rgrp.addView(mFlam);

    layout.addView(rgrp);
  private RadioButton createMyTypeRadioButton(MyType type){

    //create using this constructor to use some of the style definitions
    RadioButton radio = new RadioButton(this, null, R.style.MyRadioStyle);

    RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
    radio.setLayoutParams(layoutParams);
    radio.setGravity(Gravity.CENTER);

    //tag used by the setOnCheckedChangeListener to link the radio button with mytype object
    radio.setTag(type.getId());

    //enforce enabled and clickable status otherwise they ignore clicks
    radio.setClickable(true);
    radio.setEnabled(true);

    radio.setText(type.getTitle());


    return radio;
}

private void updateMyTypesUi() {

    //populate RadioGroup with permitted my types
    for (int i = 0; i < myTypes.size(); i++) {
        MyType type = myTypes.get(i);           
        RadioButton radioButton = createSwapTypeRadioButton(type);
        myRadioGrp.addView(radioButton);

    }

    myRadioGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton checkedType = (RadioButton) group.findViewById(checkedId);              
            String idOfMyTypeChecked = (String) checkedType.getTag();
            //do something with idOfMyTypeChecked
        }
    });
}