【问题标题】:Android: Create special RadioButton programmaticallyAndroid:以编程方式创建特殊的 RadioButton
【发布时间】:2018-07-24 07:00:54
【问题描述】:

我正在尝试创建一种比例视图。因此,我将 RadioButtons 与 RadioButton 顶部的文本一起使用。

在我的布局文件中定义这个可以正常工作:

<LinearLayout
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <RadioButton
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:text="Text on top"
            android:button="@null"
            android:drawableBottom="@android:drawable/btn_radio"
            android:gravity="center"
            android:layout_weight="1"/>
        <RadioButton
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:text="Text on top"
            android:button="@null"
            android:drawableBottom="@android:drawable/btn_radio"
            android:gravity="center"
            android:layout_weight="1"/>
    </RadioGroup>
</LinearLayout>

结果如下:

现在我只需要 RadioGroup。我想创建 RadioButtons 以编程方式 并将它们添加到 RadioGroup。

我试过这段代码,但它不起作用:

RadioButton rb = new RadioButton(Context);
LinearLayout.LayoutParams rbParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
rb.SetButtonDrawable(null);
rb.SetCompoundDrawablesWithIntrinsicBounds(null,null,null, Resources.GetDrawable(Android.Resource.Drawable.ButtonRadio));
rb.Text = "Test";
rb.Gravity = GravityFlags.Center;
rbParams.Weight = 1;
rb.LayoutParameters = rbParams;
radioGroup.AddView(rb);

我认为SetButtonDrawable 和/或SetCompoundDrawableandroid:buttonandroid:drawableBottom 不同。

编辑: 仅使用代码,而不在 .axml 文件中创建 RadioButtons,我只得到没有 RadioButton 的文本“Test”。

编辑 2:

使用 SetCompoundDrawablesWithIntrinsicBounds 我看到 RadioButton 但文本未居中。

编辑 3:

好的,使用 Mike 的 cmets(在代码中编辑)它正确显示了一个单选按钮

现在下一个问题:

因为我需要添加超过 1 个单选按钮,所以我尝试了一个使用上面相同代码的简单循环。

   for (int i = 0; i < 4; i++)
  {
        RadioButton rb = new RadioButton(Context);
        ...
        radioGroup.AddView(rb);
   }

问题是,它仍然只显示一个 RadioButton。唯一发生的事情是,RadioButton 和文本之间出现了一点空间。

编辑 4:

使用LayoutParams.WrapContent 我可以显示所有单选按钮。但我的意图是,RadioButtons 填满了 RadioGroup。这就是为什么我使用match_parent 作为宽度并认为weight=1 每个 RadioButton 将获得相同的空间。

编辑 5:

将 LayoutParameters 的宽度设置为 0 在我创建 RadioButtons 的循环内不起作用。如果这样做,则不会显示单选按钮。

什么有效: 在 for 循环中创建 RadioButtons 后,我调用此代码:

var count = radioGroup.ChildCount;

for (int i = 0; i < count; i++)
{
    var currentRb = radioGroup.GetChildAt(i);

    LinearLayout.LayoutParams rbParams2 = new LinearLayout.LayoutParams(0,
        ViewGroup.LayoutParams.WrapContent);
    rbParams2.Weight = 1;

    currentRb.LayoutParameters = rbParams2;
}

我解释这种行为的唯一想法是,RadioGroup 不知道它将拥有多少视图,因此它无法处理刚刚被膨胀的元素的权重属性。

但是谢谢Mike一步一步找到这个解决方案!

【问题讨论】:

  • 代码到底是怎么不工作的?
  • 将您的电话改为SetCompoundDrawablesWithIntrinsicBounds()
  • 您需要在RadioButton 本身而不是LayoutParams 上设置重力。
  • 对于LayoutParams 的两个参数,您都需要WrapContent,而不是MatchParent。正如您现在所拥有的那样,第一个 RadioButton 正在填充其父级,因此它过大,并将其他的推到一边。
  • 哦,然后将0 传递给第一个LayoutParams 参数;那么它会尊重重量。

标签: c# android xamarin.android radio-button android-radiobutton


【解决方案1】:

我认为您忘记将布局参数设置为单选按钮。只需在将 rb 添加到 radioGroup 之前添加 rb.setLayoutParams(rbParams);

【讨论】:

    【解决方案2】:

    适合我的解决方案:

        foreach (var option in options)
        {
            RadioButton rb = new RadioButton(Context);
            rb.SetButtonDrawable(null);
            rb.SetCompoundDrawablesWithIntrinsicBounds(null,null,null,ContextCompat.GetDrawable(Context, Android.Resource.Drawable.ButtonRadio));
            rb.Text = option.Text;
            rb.Gravity = GravityFlags.Center;
            radioGroup.AddView(rb);
        }
    
        // Weight für die RBs setzen
        for (int i = 0; i < radioGroup.ChildCount; i++)
        {
            var currentRb = radioGroup.GetChildAt(i);
            LinearLayout.LayoutParams rbParams = new LinearLayout.LayoutParams(0,
                ViewGroup.LayoutParams.WrapContent) {Weight = 1};
            currentRb.LayoutParameters = rbParams;
        }
    

    .axml 中的简化 RadioGroup:

        <LinearLayout
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:gravity="center"
        android:layout_width="match_parent"
        android:id="@+id/formItemScaleLayout"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
            <TextView
                android:id="@+id/formItemScaleStart"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Start"/>
    
            <RadioGroup
                android:layout_weight="1"
                android:id="@+id/formItemScaleRadioGroup"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >
            </RadioGroup>
    
            <TextView
                android:id="@+id/formItemScaleEnd"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="End"/>
    </LinearLayout>
    

    【讨论】:

    • 好的,如果LinearLayout 里面唯一的东西是RadioGroup,那么LinearLayout 是不必要的。去掉LinearLayout,在RadioGroup上设置layout_weight,并将其layout_width设置为0dp
    • 确保TextViews 的layout_widths 也是wrap_content。如果这不能解决问题,我就没主意了。
    • 您的代码实际上可以优化,您无缘无故地运行了两个循环
    • @MikeM。你说得对,我的布局可以简化。但是,我仍然无法在创建它们的同一循环中设置单选按钮的布局。然而,感谢您的帮助,我对这个解决方案很满意,可以继续这样做:)
    • 好的,最后一件事:每当你使用权重时,你总是希望将对应于父级方向的维度设置为0dp。我在上面提到过,但只是注意到你在RadioGroup 上仍然是layout_widthmatch_parent。干杯!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 2018-04-29
    • 2019-04-26
    • 2016-04-05
    相关资源
    最近更新 更多