簡體   English   中英

在AppBar的中心設置文本

[英]Set text in the centre of AppBar

我這是我的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>

我想在AppBar的中心做TextView。 但是我得到這個: 在此處輸入圖片說明

謝謝你的幫助。 :)

AppBar實際上是Linearlayout 嘗試這個:

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

只需將此代碼用於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>

您可以做的是在AppBar內放置一個RelativeLayout,並將其高度和寬度設置為“ match_parent”,然后將TextView放在RelativeLayout內,使其成為

android:layout_centerInParent="true"

希望這可以幫助。

嘗試這個

<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