【问题标题】:Android vertically align text and widgetAndroid垂直对齐文本和小部件
【发布时间】:2016-06-10 11:43:13
【问题描述】:

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

如何设置,使标签 (Sex) 出现在单选组的垂直中心?目前看来太高了

我的这部分布局的代码是:

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

【问题讨论】:

    标签: android android-layout android-widget android-xml


    【解决方案1】:

    我认为您可以在 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>
    

    【讨论】:

      【解决方案2】:

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

      android:layout_gravity="center_vertical"

      【讨论】:

        【解决方案3】:

        看看这个!

        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>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-06-03
          • 2020-10-13
          • 2020-07-11
          • 2015-10-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多