带抽屉导航的 Android 中心标题

标签 android android-toolbar navigation-drawer

我目前有一个带标题的工具栏的抽屉导航,我希望将此标题置于工具栏的中央,但工具栏似乎没有考虑抽屉图标,如下图所示。

Off centre

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

enter image description here

那么我如何让它考虑到这个图标呢?

这是我的布局:

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

最佳答案

您需要了解工具栏小部件扩展了 ViewGroup类,它有自己的 LayoutParams .

因此您不需要在 Toolbar 中使用 RelativeLayout,而只需添加带有 TextView 的单行。

android:layout_gravity="center_horizontal"

最终的 xml 应该是这样的,

<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 中心标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33664466/

相关文章:

c# - 如何解决 Xamarin.Android Java 互操作的缓慢问题

android - DeviceInfo native 模块未正确安装在 React Native 中

android - Apk 大小与下载大小

安卓 : Unable to hide Toolbar or Action bar

android - 带 AppCompatActivity 的 ActionBarDrawerToggle 和带 fragment 的工具栏后退按钮

java - 如何在 Android 客户端和 Java 服务器之间创建安全的 SSL 连接?

android - 工具栏标题全宽

android - 工具栏,onCreateOptionsMenu() 返回 false, 'up' 导航不工作

android - 应用启动时预加载一些 fragment

navigation - 如何在 vuetify 主抽屉/导航中创建子菜单