English 中文(简体)
Android 无线电组, 放射性按钮之间的分隔器
原标题:Android radiogroup, divider between radiobuttons

Radiobuttons 中,是否有一个简单的方法在 Radiogroup 中添加一个分隔符? 我尝试过使用 divider xml 属性,但似乎没有工作。 如果相关, 我的布局中的 Radiogroup 并不包含任何子子视图; 我程序上添加了 Radiobuttons 属性 。

EDIT :问题解决了。 您可以在 xml 中添加 RadioButton 以外的观点。 在这样的情况下, 您也可以用程序方式做, 但请注意您的布局参数。 Akki 的想法是正确的, 这对我有用 :

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>

这对你有用。我真的很好奇你是如何将视图加入到组视图中去的?这应该引起类推例外,不是吗?

以下是一种变通办法:

先创建一个“ 强” Shape 可绘制 < / 强”, 作为您的分隔符 。 以下是一个例子 :

<?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>

这只是一个简单的黑边框。 把它放在您的可绘图/ 文件夹中, 并命名它为“ 坚固” 客户端_ divider. xml

然后,转到您的布局上,该布局使用“强”无线电组

<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>

您也可以在您的 RadioGroup 中添加一个可绘制的形状。它取决于您,如果需要,请自定义它。) :

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

创建显示分隔符的形状绘图( 调用“ radio_ group_ divider ” ) :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <size android:width="12dp" />
    <solid android:color="@color/transparent" />
</shape>

在无线电组中使用此可绘图的“ divider ” :

<RadioGroup
        ...
        android:divider="@drawable/radio_group_divider"
        android:showDividers="middle"
        ...>
</RadioGroup>

如果您用程序创建 RadioGroup, 那么您也必须用程序设置分隔器。 您可以这样做 :

RadioGroup radioGroup = new RadioGroup(getActivity()); //We create a new radio group
radioGroup.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
radioGroup.setDividerDrawable(getResources().getDrawable(android.R.drawable.divider_horizontal_textfield, getActivity().getTheme()));

.

< 加强 > 注: 此代码部件正在以碎片形式工作 。 所以, 如果您想要将其放入一项活动, 您需要修改“ 以您的活性Name. this” 来修改“ 获取活动性( ) ” 。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签