2

ラジオグループにそのように見える2つのラジオボタンがあります。 ここに画像の説明を入力してください

             <RadioGroup
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" >

                    <RadioButton
                        android:id="@+id/rb_PubAsMe"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:checked="true"
                        android:text="Self"
                        android:textColor="#000000" />

                    <RadioButton
                        android:id="@+id/rb_PubAsTeam"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Team"
                        android:textColor="#000000" />
                </RadioGroup>

デフォルトでは、Androidは左揃えになりますが、これらのボタンを中央に配置したいと思います。各ボタンの重力を中央に設定しようとすると、次のようになります。

ここに画像の説明を入力してください

同じコードですが、この行を各ラジオボタンに追加しました

android:gravity="center"

誰かがこの問題に遭遇しましたか?ボタンをテキストとともに中央に移動するにはどうすればよいですか?

   |---------(Button)Text---------|---------(Button)Text---------|
4

3 に答える 3

3

これはどのように見えますか?

中央揃えのラジオボタン

いくつかの非表示のRadioButton「スペーサー」を使用しました。(以下のコード)


また、1つの「スペーサー」だけでさまざまな組み合わせを試しました...(そして、配色を模倣するために「ライト」テーマに切り替えました)。

白い中央のボタン

しかし、上の画像は下の画像よりも中央に配置され、バランスが取れているように見えます。


これはトップレイアウトのコードです。

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:orientation="horizontal" >

    <RadioButton 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:visibility="invisible" />

    <RadioButton
        android:id="@+id/rb_PubAsMe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:checked="true"
        android:text="Self" />

    <RadioButton 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:visibility="invisible" />

    <RadioButton
        android:id="@+id/rb_PubAsTeam"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Team" />

    <RadioButton 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:visibility="invisible" />

</RadioGroup>

下の画像が必要な場合は、最初と最後の「スペーサー」を削除して、layout_weight属性を削除してください。お役に立てば幸いです。

于 2012-09-05T19:14:50.670 に答える
1

コードを変更しました。

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/rb_PubAsMe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="Self"
        android:textColor="#000000" />

    <RadioButton
        android:id="@+id/rb_PubAsTeam"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Team"
        android:textColor="#000000" />

</RadioGroup>

希望どおりに動作します

于 2012-09-04T22:01:40.230 に答える
0

私が考えることができる唯一の解決策は次のとおりです。

<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:weightSum="2"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="1" >
        <RadioButton 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="true"
            android:layout_centerVertical="true"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>

    <!-- Repeat the above RelativeLayout --> 
</LinearLayout>

この方法でRadioGroupにボタンを配置することはできませんが、見た目は良さそうです。

于 2012-09-04T22:05:26.510 に答える