Toolbar title is not centered

吃可爱长大的小学妹 提交于 2019-12-06 10:07:40

问题


I added a Toolbar in my app.
For that I am using the below xml code for custom toolbar layout.

Toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar    xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#212E42"
android:minHeight="?attr/actionBarSize">

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

    <RelativeLayout
        android:id="@+id/lefttoolbarContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/righttoolbarContainer"
        android:background="#212E42">


        <TextView
            android:id="@+id/txtActionBarTitle"
            style="@style/TextViewLargeWhite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:padding="@dimen/fivedp"
            android:text="demo title for toolbar"
            android:visibility="visible" />


    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/righttoolbarContainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true">

        <ImageView
            android:id="@+id/ivActionBarRightTwo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="@dimen/fivedp"
            android:contentDescription="@string/empty_description"
            android:padding="@dimen/fivedp"
            android:src="@drawable/ic_chart"
            android:visibility="visible" />

        <ImageView
            android:id="@+id/ivActionBarRightOne"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginRight="@dimen/threedp"
            android:layout_toRightOf="@+id/ivActionBarRightTwo"
            android:contentDescription="@string/empty_description"
            android:padding="@dimen/fivedp"
            android:src="@drawable/ic_settings"
            android:visibility="gone" />



    </RelativeLayout>
</RelativeLayout>

In Android Studio output, the toolbar title is displayed in centre, as shown below.

But when I run my app, I can't get toolbar title at centre, as shown below :

This issue raises when I add the Toolbar navigation icon dynamically in my app.
After adding the navigation icon, the icon gets its space and the toolbar title goes to the right side.

 mToolbar = (Toolbar) findViewById(R.id.toolbar);
 mToolbar.setNavigationIcon(R.drawable.logo)//Toolbar navigation icon will add here
 setSupportActionBar(mToolbar);

EDIT: My Toolbar which I want to achieve is below ,For that please check updated Toolbat.xml:

Please, help me to get the Toolbar title centered!


回答1:


You can remove nested relativelayout and add an imageview to the relativelayout lefttoolbarContainer. Since your title is inParentCenter, it shouldn't move to the right when you add the image in code(atleast in theory). i.e

Toolbar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/default_dark_blue"
android:minHeight="?attr/actionBarSize">

<RelativeLayout
    android:id="@+id/lefttoolbarContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#212E42">

    <ImageView
        android:id="@+id/navigationIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="56dp"
        android:scaleType="center"
        android:layout_alignParentLeft="true"/>

    <TextView
        android:id="@+id/txtActionBarTitle"
        style="@style/TextViewLargeWhite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:padding="@dimen/fivedp"
        android:text="demo title for toolbar"
        android:visibility="visible" />


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

And in your activity/fragment

mToolbar = (Toolbar) findViewById(R.id.toolbar);
ImageView navigationIcon = mToolbar.findViewById(R.id.navigationIcon);
navigationIcon.setImageResource(R.drawable.logo);
setSupportActionBar(mToolbar);

Hope it helps..




回答2:


Try changing the textview attribute

android:gravity="center"

with this:

 android:layout_gravity="center"


来源:https://stackoverflow.com/questions/33778135/toolbar-title-is-not-centered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!