简体   繁体   English

在AppBar的中心设置文本

[英]Set text in the centre of AppBar

I this is my xml: 我这是我的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. 我想在AppBar的中心做TextView。 But I get this: 但是我得到这个: 在此处输入图片说明

Thanks for your help. 谢谢你的帮助。 :) :)

AppBar is actually a Linearlayout . AppBar实际上是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

<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 您可以做的是在AppBar内放置一个RelativeLayout,并将其高度和宽度设置为“ match_parent”,然后将TextView放在RelativeLayout内,使其成为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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