简体   繁体   中英

Why the layout gravity of TextView is not working in horizontal LinearLayout?

This is the code which i am trying. but the textview which has layout_gravity right no shifting to right in horizontal linear layout.

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:layout_gravity="left"
                android:gravity="center_vertical"
                android:text="This Year"
                android:textColor="@color/gray_dark"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/tvHomeIncomeThisYear"
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:gravity="center_vertical"
                android:layout_gravity="right"
                android:text="$0.00 USD"
                android:textColor="#000000"
                android:textSize="18sp" />

        </LinearLayout>

All children of a LinearLayout are stacked one after the other. For your goal you can use FrameLayout, or you can add space as another child view object:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_gravity="left"
            android:gravity="center_vertical"
            android:text="This Year"
            android:textColor="@color/gray_dark"
            android:textSize="18sp" />

        <Space
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/tvHomeIncomeThisYear"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:gravity="center_vertical"
            android:layout_gravity="right"
            android:text="$0.00 USD"
            android:textColor="#000000"
            android:textSize="18sp" />
 </LinearLayout>

Space requires API level 14 or above. Otherwise you can use View .

Change LinearLayout to RelativeLayout and android:layout_gravity="right" to android:layout_alignParentRight="true" . Remove android:orientation="horizontal" .

Use it using Relative layout

<RelativeLayout
   android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_gravity="left"
            android:gravity="center_vertical"
            android:text="This Year"
            android:textColor="@color/gray_dark"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tvHomeIncomeThisYear"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:gravity="center_vertical"
            android:layout_alignParentRight="true" 
            android:text="$0.00 USD"
            android:textColor="#000000"
            android:textSize="18sp" />
</RelativeLayout>

Just change the TextView tvHomeIncomeThisYear width to match_parent and set it's gravity and layout_gravity to right|center_vertical and center_vertical respectively :

        <TextView
            android:id="@+id/tvHomeIncomeThisYear"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="right|center_vertical"
            android:layout_gravity="center_vertical"
            android:text="$0.00 USD"
            android:textColor="#000000"
            android:textSize="18sp" />

You don't actually need to change the parent view to something other than a LinearLayout, or insert additional views to act as spacers etc., just add a layout_weight property to your leftmost TextView with a value of "1" and set it's layout_width to zero (0dp):

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:text="This Year"
        android:textColor="@color/gray_dark"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/tvHomeIncomeThisYear"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:gravity="center_vertical"
        android:text="$0.00 USD"
        android:textColor="#000000"
        android:textSize="18sp" />

</LinearLayout>

You can then also remove both TextView's android:layout_gravity property, as they are not needed.

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