簡體   English   中英

Android中RadioButton的自定義對齊

[英]Custom Alignment of RadioButtons in Android

我有4個RadioButton(屬於同一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如何使用這種類型的對齊方式保留其屬性嗎?

您可以嘗試一下...。

首先聲明所有單選按鈕

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

嘗試下面的代碼對我來說效果很好:-

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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM