Android 垂直对齐文本和小部件

Android vertically align text and widget

我在水平线性布局中使用文本标签和单选按钮组:

如何设置标签(性别)出现在单选组的垂直居中?目前看来太高了

我这部分布局的代码是:

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

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.4"
            android:text="Sex:" />

        <RadioGroup
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.6"
            android:orientation="horizontal">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Male"
                android:id="@+id/input_button_male"
                android:paddingRight="10dp"/>

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Female"
                android:id="@+id/input_button_female"
                android:paddingRight="10dp"/>
        </RadioGroup>

    </LinearLayout>

我认为您可以在 Textview 中使用 android:layout_marginTop="??"。 或者,您可以在 Textview 中使用 android:gravity="center_vertical"。应该是后者。

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

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.4"
        android:layout_gravity="center_vertical"
        android:text="Sex:" />

    <RadioGroup
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.6"
        android:orientation="horizontal">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Male"
            android:id="@+id/input_button_male"
            android:paddingRight="10dp"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Female"
            android:id="@+id/input_button_female"
            android:paddingRight="10dp"/>
    </RadioGroup>

</LinearLayout>

你想要这样吗然后把你的文本视图放在代码下面

android:layout_gravity="center_vertical"

看看这个!

android:layout-width="0dp" not recommended! Prefer androd:layout-width="wrap_content"

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

<TextView
    android:layout_marginLeft="48dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|center_vertical"
    android:layout_weight="0.4"
    android:text="Sex :"
    android:textSize="20sp" />

<RadioGroup
    android:layout_marginRight="72dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.6"
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/input_button_male"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="10dp"
        android:text="Male" />

    <RadioButton
        android:id="@+id/input_button_female"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="10dp"
        android:text="Female" />
</RadioGroup>