简体   繁体   中英

Set text in the centre of AppBar

I this is my xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_height="57dp"
        android:layout_width="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Ranger"
            android:id="@+id/logoSign"/>
    </android.support.design.widget.AppBarLayout>
</RelativeLayout>

I want to do the TextView in the centre of the AppBar. But I get this: 在此处输入图片说明

Thanks for your help. :)

AppBar is actually a Linearlayout . Try this:

   <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Ranger"
        android:id="@+id/logoSign"/>

just use this code for textview

<TextView
        android:layout_width="match_content"
        android:layout_height="match_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Ranger"
        android:id="@+id/logoSign"/>
<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_height="57dp"
    android:layout_width="match_parent">
    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Ranger"
            android:id="@+id/logoSign"/>
    </LinearLayout>
</android.support.design.widget.AppBarLayout>

What you can do is Take a RelativeLayout inside the AppBar with height and width as "match_parent" and put the TextView inside the RelativeLayout and make it as

android:layout_centerInParent="true"

Hope this helps.

Try this

<TextView
        android:layout_width="wrap_content"
        android:layout_height="match_content"
        android:layout_gravity="center_horizontal|center_vertical"
        android:text="Ranger"
        android:id="@+id/logoSign"/>

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