Android radiogroup,单选按钮之间的分隔符

Android radiogroup, divider between radiobuttons(Android radiogroup,单选按钮之间的分隔符)

本文介绍了Android radiogroup,单选按钮之间的分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种简单的方法可以在 RadioGroup 内的 RadioButtons 之间添加分隔线?我尝试使用 divider xml 属性,但它似乎不起作用.如果相关,我的布局中的 RadioGroup 不包含任何子视图;我正在以编程方式添加 RadioButtons.

Is there a simple way to add a divider between RadioButtons inside a RadioGroup? I've tried using the divider xml attribute and it doesn't seem to be working. In case it's relevant, the RadioGroup in my layout does not contain any child views; I'm adding the RadioButtons programmatically.

编辑:问题已解决.您可以在 xml 中的 RadioGroup 内添加除 RadioButton 之外的视图.在我的情况下,您也可以通过编程方式执行此操作,但请注意您的布局参数.Akki 有正确的想法,这对我有用:

EDIT: Problem solved. You can add views besides RadioButton inside RadioGroup in the xml. In my case, you can also do it programmatically, but be careful about your layout params. Akki had the right idea, and this worked for me:

for (int i = 0; i < items.size(); i++) {
    if (i > 0) {
        // add a divider with height of 1 pixel
        View v = new View(this);
        v.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.MATCH_PARENT, 1));
        v.setBackgroundColor(android.R.color.darker_gray);
        mRadioGroup.addView(v);
    }
    RadioButton rb = new RadioButton(this);
    /* set other properties ... */

    mRadioGroup.addView(rb);
}

推荐答案

<RadioGroup
    android:id="@+id/location_radio_group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="?android:attr/dividerHorizontal"
    android:showDividers="middle">
</RadioGroup>

这对你有用.我真的很好奇您如何将视图添加到组视图中?那应该导致classcastexception,不是吗?

That will work for you. And I am really curious how you add view into Group View? That should cause classcastexception, no ?

这篇关于Android radiogroup,单选按钮之间的分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android radiogroup,单选按钮之间的分隔符

基础教程推荐