简体   繁体   中英

How can I locate TextView on center LinearLayout?

I am trying to locate TextView on center (vertical, horizontal) LinearLayout, but It does not work. I can locate only center horizontally or center on vertically, but i need locate center horizontally and center on vertically. Could you please explain how i can do this?

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">

    <LinearLayout
        android:id="@+id/sigpanel"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/button_panel"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp"
        android:layout_alignParentStart="true"
        android:baselineAligned="false"
        android:background="@drawable/customborder"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_Signature"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Signature"
            android:layout_gravity="center"
            android:textColor="#002060" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/button_panel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <Button
            android:id="@+id/button_next"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="15dip"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dip"
            android:layout_weight="1"
            android:background="@drawable/gradient_green"
            android:drawableLeft="@drawable/ic_next"
            android:gravity="center"
            android:shadowColor="#014927"
            android:shadowDx="0.0"
            android:shadowDy="0.0"
            android:shadowRadius="2.5"
            android:text="Next"
            android:textColor="#001B51"
            android:textSize="20sp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/button_cancel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="15dip"
            android:layout_marginRight="10dip"
            android:layout_weight="1"
            android:background="@drawable/gradient_red"
            android:drawableLeft="@drawable/ic_cancel"
            android:gravity="center"
            android:shadowColor="#014927"
            android:shadowDx="0.0"
            android:shadowDy="0.0"
            android:shadowRadius="2.5"
            android:text="Clear"
            android:textColor="#001B51"
            android:textSize="20sp"
            android:textStyle="bold" />
    </LinearLayout>

</RelativeLayout>

Add gravity center or center_vertical|clip_horizontal in LinearLayout of TextView as following:

android:gravity="center"

OR

android:gravity="center_vertical|clip_horizontal"

Example:

<LinearLayout
    android:id="@+id/sigpanel"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/button_panel"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="10dp"
    android:layout_alignParentStart="true"
    android:gravity="center"
    android:baselineAligned="false"
    android:background="@drawable/customborder"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_Signature"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Signature"
        android:layout_gravity="center"
        android:textColor="#002060" />
</LinearLayout>

Hey use relative layout in linear or try this

        android:id="@+id/tv_Signature"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Signature"
        android:gravity="center"
        android:textColor="#002060"

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