【问题标题】:Custom Alignment of RadioButtons in AndroidAndroid中单选按钮的自定义对齐方式
【发布时间】:2014-06-17 16:43:41
【问题描述】:

我有 4 个 RadioButtons(同一个 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 失去了它的属性,所有的 RadioButtons 可以同时被检查

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

【问题讨论】:

  • @Hemant :我希望一次只检查一个单选按钮(因为它们是 1 RadioGroup 的一部分)
  • @Amardeep : 这段代码只是我用来制作广播组的 XML 的一部分
  • 检查我更新的答案... android:checked="true" 用于任何单选按钮
  • 请给出否决这个问题的理由!或者你总是对你无法回答的问题投反对票?

标签: android xml radio-button alignment


【解决方案1】:

尝试下面的代码在我的最后可以正常工作:-

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

【讨论】:

  • 我问“知道 RadioGroup 如何使用这种对齐方式保留其属性吗?”我为此问题添加了评论,以便每个人都可以在那里阅读它
【解决方案2】:

使用此代码:

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

【讨论】:

  • 请查看我添加到问题中的评论
  • 您是否完整阅读了问题? --> 我希望一次只检查一个单选按钮(因为它们是 1 RadioGroup 的一部分)
  • 1 RadioGroup 有 4 个 RadioButtons 对齐,如图所示 问题是 --> RadioGroup 由于对齐而失去其属性“只能检查 1 个按钮”。正在寻找的解决方案 --> 具有相同的对齐方式,但 RadioGroup 仍保留其属性。现在清除??
【解决方案3】:

你可以试试这个......

首先声明所有单选按钮

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

现在 - onclick 监听器做

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

【讨论】:

  • 是的,它可以使用 java 代码来完成,但有没有办法只从 XML 中完成?
猜你喜欢
  • 2011-05-12
  • 2017-06-26
  • 2021-03-27
  • 1970-01-01
  • 1970-01-01
  • 2020-05-07
  • 2017-01-16
  • 2015-10-04
  • 1970-01-01
相关资源
最近更新 更多