【问题标题】:Inflating Nested Layout with RadioButton to a Container将带有 RadioButton 的嵌套布局膨胀到容器
【发布时间】:2018-05-28 07:09:18
【问题描述】:

我创建了一个布局,该布局将膨胀到该布局包含单选按钮的容器中

问题:布局膨胀,但所有单选按钮都被选中,这是错误的吗?

包含要在容器中膨胀的单选按钮的布局。

<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="23dp"
   android:layout_marginTop="10dp"
   android:orientation="horizontal">
   <RadioButton
    android:id="@+id/radioButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</LinearLayout>

容器布局

<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
   <RadioGroup
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</LinearLayout>

给孩子充气的代码

 LayoutInflater inflater = (LayoutInflater)   getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    for (int i = 0 ; i < 6 ; i++){
        LinearLayout child =(LinearLayout) inflater.inflate(R.layout.layout_linear_with_rb,container,false);
        container.addView(child);
    }

截图:RadioButton screenshot containing someview

【问题讨论】:

    标签: android android-layout radio-button radio-group android-inflate


    【解决方案1】:

    广播组documentation

    选择由 XML 布局文件中定义的单选按钮的唯一 ID 标识。

    查看您的代码库,我发现您的 RadioButton 没有唯一的 Id。

    我制作了一个示例项目并尝试使用唯一 ID 动态添加 RadioButton,它可以完美运行。

        RadioGroup container = findViewById(R.id.container);
    
        for (int i = 0 ; i < 6 ; i++){
            RadioButton radioButton = new RadioButton(this);
            radioButton.setId(i);
            container.addView(radioButton);
        }
    

    在这种情况下,可能会出现 id 冲突的问题。也许在其他视图上设置了 0 的 id。为了避免这种混淆,我建议使用 View.generateViewId() 来生成唯一的 id。

    View.generateViewId() 仅适用于 API >= 17。

    编辑 1

    请停止在 RadioButton 布局中使用 LinearLayout 作为父级。一个快速的解决方法是将 RadioButton 布局文件更改为

    <RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/radioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    

    并将您的 Java 代码更改为

    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    for (int i = 0 ; i < 6 ; i++){
        RadioButton radioButton = (RadioButton) inflater.inflate(R.layout.layout_linear_with_rb,container,false);
        radioButton.setId(i);
        container.addView(radioButton);
    }
    

    【讨论】:

    • 我有一个问题,作为 RadioButton 父级的 LinearLayout 包含一些视图,我没有将它包含在我的问题中对不起。
    • 尝试像我对 LinearLayouts 所做的那样放置唯一的 id。
    • 我添加了我的单选按钮布局的屏幕截图
    • 我想没有直接的方法可以实现您想要做的事情。这个链接可能有点用stackoverflow.com/a/13273890/2356570
    • 在问题中添加您的答案。它会帮助别人。
    【解决方案2】:

    这里的问题是 RadioGroup 它仅查找 RadioButton 子项,并且因为我使用包含 RadioButton 的嵌套布局,所以 RadioGroup 无法找到以编程方式膨胀的 RadioButton。

    我如何解决这个问题是 https://github.com/worker8/RadioGroupPlus 这是一个调整 RadioGroup,它深入挖掘嵌套布局并在其中找到 RadioButton。

    【讨论】:

      猜你喜欢
      • 2011-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-21
      • 1970-01-01
      相关资源
      最近更新 更多