如何使用LinearLayout将TextView对齐到右侧

Jim*_*imT 1 layout android

我有两个textView对象,我用它来显示我的应用程序中的计算结果.它们显示在TextView的右侧,因此我想知道是否有一种方法可以通过xml文件将TextView定位到右侧.这是我的xml代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.jtryon.rectanglecalc.MainActivity$PlaceholderFragment" >

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/width_string"
            android:textSize="16sp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/width_edit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"      
            android:inputType="numberDecimal" > 

        <requestFocus />
        </EditText>       

    </LinearLayout>

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/height_string"
        android:textSize="16sp"
        android:textStyle="bold" /> 

    <EditText
        android:id="@+id/height_edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="numberDecimal" />     

    </LinearLayout>

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:textStyle="bold" 
            android:text="@string/area_string" />

        <TextView
            android:id="@+id/area_value"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp" />

    </LinearLayout>

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

        <TextView
            android:id="@+id/perim_string"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:textStyle="bold"
            android:text="@string/perim_string" />


        <TextView
            android:id="@+id/perim_value"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp" />

        </LinearLayout>    

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

应该怎么样

Mar*_*ira 11

确保您的视图与父视图匹配,然后将其内容的重力添加到右侧:

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="@string/width_string" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

希望它有所帮助;)

  • `TextView`将占据整个宽度,`gravity'应该用于移动文本而不是`layout_gravity`.无论如何,我不确定这是OP想要的. (2认同)