带导航抽屉的Android中心标题 [英] Android centre title with navigation drawer

查看:73
本文介绍了带导航抽屉的Android中心标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个带有标题的工具栏的导航抽屉,我希望将此标题居中放置在工具栏的中心,但是正如您在下图中看到的那样,工具栏似乎并未考虑到抽屉图标.

I currently have a navigation drawer with a toolbar that has a title, i wish to centre this title within the toolbar but the toolbar does not seem to take into consideration the drawer icon as you can see in the following picture.

而当我对不在导航抽屉内的其他活动使用相同的工具栏布局时,标题将完美居中,如下图所示:

whereas when i use the same toolbar layout for other activities that are not inside the navigation drawer the title is centred perfectly, as show in this picture:

那么我如何考虑到该图标呢?

So how do i get it to take into account this icon?

这是我的布局:

<android.support.design.widget.AppBarLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:theme="@style/Theme.App.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/black"
        app:popupTheme="@style/Theme.App.PopupOverlay">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:textColor="@color/white"
                android:textStyle="bold"
                android:text="Title"
                android:textSize="16sp" />

        </RelativeLayout>

    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

推荐答案

您需要了解工具栏小部件扩展了 LayoutParams .

You need to understand that Toolbar widget extends ViewGroup class, and it has it's own LayoutParams.

因此,您不需要在工具栏中添加RelativeLayout,而只需在TextView中添加一行即可.

So you do not require RelativeLayout inside Toolbar, and need to add just single line with TextView.

android:layout_gravity="center_horizontal"

最终的xml应该看起来像这样,

And final xml should look like this,

<android.support.design.widget.AppBarLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.App.AppBarOverlay" >

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/black"
    app:popupTheme="@style/Theme.App.PopupOverlay" >

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Title"
        android:textColor="@color/white"
        android:textSize="16sp"
        android:textStyle="bold" />
</android.support.v7.widget.Toolbar>

这篇关于带导航抽屉的Android中心标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆