简体   繁体   中英

How to set text view aligned to parent textview inside relative layout in android?

<RelativeLayout
    android:id="@+id/rl_top_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text1"
        style="@style/Typeface.H1.Bold.TextDarkGrey"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="left"
        android:text="500pts"
        android:textColor="@color/text_dark_grey"
        android:textSize="@dimen/tfl_24"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/text2"
        style="@style/Typeface.H1.Bold.TextDarkGrey"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="right"
        android:text="£5.00"
        android:textColor="@color/text_dark_grey"
        android:textSize="@dimen/tfl_24"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/text3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/fdv_text_points"
        android:layout_gravity="center"
        android:gravity="left"
        android:text="@string/points"
        android:textColor="@color/text_mid_grey"
        android:textSize="@dimen/tfl_16" />

    <TextView
        android:id="@+id/text4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/fdv_text_voucher"
        android:layout_gravity="center"
        android:gravity="right"
        android:text="@string/voucher"
        android:textColor="@color/text_mid_grey"
        android:textSize="@dimen/tfl_16" />
</RelativeLayout>

This is my xml i have set two text view left and right and also below two more text view left and right i want below text view should aligned to parent top text view like text1 aligned center to text3 and text2 aligned to text 4 currently its coming text1 start to text3 and text2 start of text 4 please suggest me how to set aligned center of parent

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="fkaj;sdlfkasj;dfksaf"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="asdf"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="fkaj;sdlfkasj;dfksaf"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="asdf"/>

    </LinearLayout>

</LinearLayout>

Actually layout gravity does not work in Relative layout so should use layout_below, layout_left_of, layout_right_of and layout_above property of Relative Layout. You can also use centerInParent, AlignParentLeft, AlignParentRight property of Relative Layout instead of gravity

@Khemraj Answer is the desired output what you need. But you need in relative layout then try below code

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_toLeftOf="@id/divider2"
        android:layout_toStartOf="@+id/divider2"
        android:gravity="center"
        android:text="Title More Text" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/tv1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/tv1"
        android:layout_below="@+id/tv1"
        android:gravity="center"
        android:text="description" />


    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_toEndOf="@+id/divider2"
        android:layout_toRightOf="@id/divider2"
        android:gravity="center"
        android:text="Title More Text" />

    <TextView
        android:id="@+id/tv4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/tv3"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignStart="@+id/tv3"
        android:layout_below="@+id/tv1"
        android:gravity="center"
        android:text="decsription" />

    <View
        android:id="@+id/divider2"
        android:layout_width="1dp"
        android:layout_height="1dp"
        android:layout_centerHorizontal="true"
        android:background="?android:attr/listDivider" />

</RelativeLayout>

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