【问题标题】:How to align text left to RadioGroup in Android?如何在Android中将文本左对齐到RadioGroup?
【发布时间】:2014-07-21 17:45:07
【问题描述】:

虽然this Q&A已被勾选,但文本无法左对齐Android中的RadioGroup。

目前的成果

预期结果

代码

C:\workspace-adt\Helloworld\res\layout\activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.helloworld.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:text="@string/fractions" />

    <RadioGroup
        android:id="@+id/fractions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/fraction_true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:paddingRight="15dip"
            android:text="@string/fraction_true" />

        <RadioButton
            android:id="@+id/fraction_false"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="15dip"
            android:text="@string/fraction_false" />
    </RadioGroup>

</RelativeLayout>

C:\workspace-adt\Helloworld\res\values\strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Helloworld</string>
    <string name="fractions">Fractions</string>
    <string name="action_settings">Settings</string>
    <string name="fraction_true">True</string>
    <string name="fraction_false">False</string>

</resources>

【问题讨论】:

    标签: android


    【解决方案1】:

    似乎是水平线性布局的情况。

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:text="lalalal" />
    
        <RadioGroup
            android:id="@+id/fractions"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <RadioButton
                android:id="@+id/fraction_true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:paddingRight="15dip"
                android:text="lalalal" />
    
            <RadioButton
                android:id="@+id/fraction_false"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingRight="15dip"
                android:text="lalalal" />
        </RadioGroup>
    
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      Linear Layoutsweight 一起使用。这样会更简单

      查看文档

      Layouts

      或者只用LinearLayout

      <LinearLayout
                 android:id="@+id/linearLayout5" 
                 android:layout_width= "fill_parent"
                 android:orientation="horizontal"
                 android:layout_gravity="center"
                >
       <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:text="@string/fractions" />
                 <RadioGroup
                     android:id="@+id/radioGroup1"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:orientation="horizontal" >
      
                     <RadioButton
              android:id="@+id/fraction_true"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:checked="true"
              android:paddingRight="15dip"
              android:gravity="center"
              android:layout_gravity="center"
              android:text="lalalal" />
      
          <RadioButton
              android:id="@+id/fraction_false"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:paddingRight="15dip"
              android:gravity="center" 
              android:layout_gravity="center"
              android:text="lalalal" />
      
                 </RadioGroup>
      
      
      
              </LinearLayout> 
      

      【讨论】:

        猜你喜欢
        • 2012-05-02
        • 2021-09-27
        • 2021-10-05
        • 1970-01-01
        • 1970-01-01
        • 2014-12-12
        • 2013-03-26
        • 1970-01-01
        相关资源
        最近更新 更多