简体   繁体   English

带导航抽屉的 Android 中心标题

[英]Android centre title with navigation drawer

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?那么我如何让它考虑到这个图标?

Here is my layout:这是我的布局:

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

You need to understand that Toolbar widget extends ViewGroup class, and it has it's own LayoutParams .您需要了解 Toolbar 小部件扩展了ViewGroup类,并且它有自己的LayoutParams

So you do not require RelativeLayout inside Toolbar, and need to add just single line with TextView.所以你不需要在 Toolbar 中使用 RelativeLayout,只需要添加一行 TextView。

android:layout_gravity="center_horizontal"

And final xml should look like this,最终的 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>

Remove the RelativeLayout , then change your TextView like this:删除RelativeLayout ,然后像这样更改您的TextView

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

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

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

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

After that call setDisplayShowTitleEnabled , to remove the original title, in the onCreate method of your activity:在调用setDisplayShowTitleEnabled之后,在活动的onCreate方法中删除原始标题:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        ...
    }
}

This is the result obtained:这是得到的结果:

在此处输入图片说明

您需要在 Textview 中将android:layout_width="wrap_content"更改为android:layout_width="match_parent" .....

Hope this will help.希望这会有所帮助。 try to use this code into your xml , your code with few changes :尝试将此代码使用到您的 xml 中,您的代码几乎没有更改:

在此处输入图片说明

<TextView
        android:id="@+id/toolbar_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="title"
        android:gravity="center"
        android:textColor="#ffffff"
        android:textSize="21dp"
        android:textStyle="bold"
        android:paddingRight="30dp"
        android:paddingLeft="10dp"
        android:paddingTop="15dp" />

Use this for each activity.将其用于每个活动。 This will makes sure that your text would cover the entire space next to the icon and center the text within it.这将确保您的文本将覆盖图标旁边的整个空间并将文本置于其中。

<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
    android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:paddingTop="15dp"
        android:src="@drawable/ic_keyboard_arrow_left_black_24dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="title"
        android:gravity="center"
        android:textColor="#ffffff"
        android:textSize="21dp"
        android:textStyle="bold"
        android:paddingRight="30dp"
        android:paddingLeft="10dp"
        android:paddingTop="15dp" />

</RelativeLayout>

--------Nav_Drawer layout 

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height"
        android:background="@drawable/side_nav_bar"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:theme="@style/ThemeOverlay.AppCompat.Dark" android:orientation="vertical"
        android:gravity="bottom">

        <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:paddingTop="@dimen/nav_header_vertical_spacing"
            android:src="@android:drawable/sym_def_app_icon" android:id="@+id/imageView" />

        <TextView android:layout_width="match_parent" android:layout_height="wrap_content"
            android:paddingTop="@dimen/nav_header_vertical_spacing" android:text="Android Studio"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="android.studio@android.com" android:id="@+id/textView" />

    </LinearLayout>

For me, it did not work either of the answers.对我来说,这两个答案都不起作用。 Instead, I just put to the TextView a margin start of -40dp which is the icon width of the drawer.相反,我只是将-40dp的边距开始添加到 TextView,这是抽屉的图标宽度。 And it's working great.而且效果很好。 Maybe it's useful for someone也许它对某人有用

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

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