Android的RadioGroup中,单选按钮之间的分隔 [英] Android radiogroup, divider between radiobuttons

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

问题描述

有没有一种简单的方式来增加与单选按钮分频器在 RadioGroup中?我已经使用了 XML属性尝试,它似乎并不奏效。如果它是相关的,在 RadioGroup中在我的布局不包含任何孩子的意见;我添加了单选按钮编程。

修改:问题解决了。您可以添加除了单选的意见里面 RadioGroup中的XML。在我的情况,你也可以做到这一点编程,但要小心你的布局PARAMS。 Akki是对的,而这个工作对我来说:

 的for(int i = 0; I< items.size();我++){
    如果(ⅰ大于0){
        //添加一个分频器1像素的高度
        视图V =新景(本);
        v.setLayoutParams(新RadioGroup.LayoutParams(LayoutParams.MATCH_PARENT,1));
        v.setBackgroundColor(android.R.color.darker_gray);
        mRadioGroup.addView(五);
    }
    单选RB =新的单选按钮(这一点);
    / *设置其他属性... * /

    mRadioGroup.addView(RB);
}
 

解决方案

下面是一个解决方法:

首先创建一个图形绘制对象作为您的分隔。下面是一个例子:

 < XML版本=1.0编码=UTF-8&GT?;
<形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:形状=矩形>
[固体
    机器人:颜色=@色/白/>
<中风
    机器人:宽=0.3dp
    机器人:颜色=@彩色/黑白/>
< /形状>
 

这只是一个简单的黑色边框。把你的绘制/文件夹内,并命名为类似的 custom_divider.xml

然后,转到你的布局,它使用一个 RadioGroup中。使用ShapeDrawable作为背景,每个单选(S)。下面是一个例子:

 < RadioGroup中
        机器人:ID =@ + ID / radioGroup1
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginTop =10dp
        机器人:分隔=@彩色/黑白>

        <单选按钮
            机器人:ID =@ + ID / radio0
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:背景=@可绘制/ custom_radiogroup_divider
            机器人:检查=真
            机器人:文本=单选/>

        <单选按钮
            机器人:ID =@ + ID /收音机1
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:背景=@可绘制/ custom_radiogroup_divider
            机器人:文本=单选/>

        <单选按钮
            机器人:ID =@ + ID / RADIO2
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:背景=@可绘制/ custom_radiogroup_divider
            机器人:文本=单选/>

    < / RadioGroup中>
 

您还可以将ShapeDrawable添加到您的RadioGroup中。这取决于你,定制它,如果你需要的。 :)

下面是我与自定义边框(与圆角半径)和自定义分隔ShapeDrawable(S)一个RadioGroup中的例子。

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.

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);
}

解决方案

Here's a workaround:

First create a Shape Drawable as your divider. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
<solid 
    android:color="@color/white" />
<stroke 
    android:width="0.3dp" 
    android:color="@color/black" />
</shape>

This is just a simple black border. Put it inside your drawable/ folder and name it something like custom_divider.xml.

Then, go to your layout which uses a RadioGroup. Use the ShapeDrawable as a background for each of the RadioButton(s). Here is an example:

<RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:divider="@color/black" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/custom_radiogroup_divider"
            android:checked="true"
            android:text="RadioButton" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/custom_radiogroup_divider"
            android:text="RadioButton" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/custom_radiogroup_divider"
            android:text="RadioButton" />

    </RadioGroup>

You can also add a ShapeDrawable to your RadioGroup. It depends on you, customize it if you need. :)

Here is my example of a RadioGroup with custom border (with corner radius) and custom divider ShapeDrawable(s).

这篇关于Android的RadioGroup中,单选按钮之间的分隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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