简体   繁体   中英

How to align a TextView to the right with LinearLayout

I have two textView objects that I am using to display the result of the calculations in my app. They are showing up to the right of the TextView so I wanted to know if there is a way through the xml file to position the TextView to the right. This is my xml code:

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

它应该看起来如何

Make sure your view matches the parent, then add the gravity of its content to the right:

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

Hope it helped ;)

Just add the line

android:gravity="right"

to the textview you want aligned left.

You can do something like this

  <LinearLayout
    android:gravity="end"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:src="@drawable/ic_call_white"
        />

</LinearLayout>

Please check that Linear Layout has an attribute named GRAVITY and inner control(ImageView) has an attribute named LAYOUT_GRAVITY both are different

Sure. Just change the order of the elements inside the LinearLayout. Make sure the EditText field is first and then the TextView field. Easy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM