Android中单选按钮的自定义对齐

Android中单选按钮的自定义对齐,android,xml,radio-button,alignment,Android,Xml,Radio Button,Alignment,我有4个单选按钮(同一个RadioGroup的一部分),我想按如下方式对齐它们: 我使用的代码是: <RadioGroup android:id="@+id/add_reminder_type" android:layout_width="match_parent" android:layout_height="wrap_content" android:g

我有4个单选按钮(同一个RadioGroup的一部分),我想按如下方式对齐它们:

我使用的代码是:

    <RadioGroup
        android:id="@+id/add_reminder_type"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="@dimen/zero"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:orientation="vertical" >

            <RadioButton
                android:id="@+id/add_reminder_daily"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/add_reminder_daily" />

            <RadioButton
                android:id="@+id/add_reminder_weekly"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/add_reminder_weekly" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="@dimen/zero"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:orientation="vertical" >

            <RadioButton
                android:id="@+id/add_reminder_monthly"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/add_reminder_monthly" />

            <RadioButton
                android:id="@+id/add_reminder_yearly"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/add_reminder_yearly" />
        </LinearLayout>
    </RadioGroup>

但是通过使用此代码,RadioGroup将丢失其属性,并且可以同时检查所有单选按钮


知道RadioGroup如何使用这种对齐方式来保留其属性吗?

试试下面的代码,在我这方面效果很好:-

 <RadioGroup
        android:id="@+id/add_reminder_type"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginLeft="10dp"
            android:gravity="center_horizontal"
            android:orientation="vertical" >

            <RadioButton
                android:id="@+id/add_reminder_daily"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Daily" />

            <RadioButton
                android:id="@+id/add_reminder_weekly"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Monthly" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:orientation="vertical" >

            <RadioButton
                android:id="@+id/add_reminder_monthly"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Weekly" />

            <RadioButton
                android:id="@+id/add_reminder_yearly"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Yearly" />
        </LinearLayout>
    </RadioGroup>

使用以下代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="horizontal" >

 <RadioGroup
    android:id="@+id/add_reminder_type"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="@dimen/zero"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <RadioButton
            android:id="@+id/add_reminder_daily"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="daily" />

        <RadioButton
            android:id="@+id/add_reminder_weekly"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="weekly" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="@dimen/zero"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <RadioButton
            android:id="@+id/add_reminder_monthly"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="monthly" />

        <RadioButton
            android:id="@+id/add_reminder_yearly"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="yearly" />
    </LinearLayout>
</RadioGroup>

</LinearLayout>

你可以试试这个

首先声明所有单选按钮

r1=(RadioButton) findViewById(R.id.add_reminder_daily);
 r2=(RadioButton) findViewById(R.id.add_reminder_weekly);
 r3=(RadioButton) findViewById(R.id.add_reminder_monthly);
 r4=(RadioButton) findViewById(R.id.add_reminder_yearly);
现在打开-单击listner do

r1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                    r2.setChecked(false);
                    r3.setChecked(false);
                    r4.setChecked(false);
            }
        });
 r2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                    r1.setChecked(false);
                    r3.setChecked(false);
                    r4.setChecked(false);
            }
        });
 r3.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                    r2.setChecked(false);
                    r1.setChecked(false);
                    r4.setChecked(false);
            }
        });
 r4.setOnClickListener(new OnClickListener() {
            @Override

            public void onClick(View v) {
                    r2.setChecked(false);
                    r3.setChecked(false);
                    r1.setChecked(false);
            }
        });

@赫曼特:我希望一次只检查一个单选按钮(因为它们是1个RadioGroup的一部分)@Amardeph:这段代码只是我的XML的一部分,用于使RadioGroupCheck成为我的更新版本。。。android:checked=“true”用于任何广播按钮请给出否决此问题的原因!或者你总是否决你不能回答的问题?我问“你知道RadioGroup如何使用这种对齐方式保留其属性吗?”我在我的问题中添加了一条注释,这样每个人都只能在那里阅读。请看我添加到我的问题中的注释。你完整地阅读了问题吗??-->我希望一次只检查一个单选按钮(因为它们是1个RadioGroup的一部分)1个RadioGroup有4个单选按钮,如图所示对齐。问题是-->RadioGroup由于对齐而丢失其属性“只能检查1个按钮”。查找-->的解决方案具有相同的对齐方式,但仍保留其属性。现在清楚了吗?是的,可以使用java代码来完成,但是有没有办法只从XML来完成?