简体   繁体   English

工具栏标题未居中

[英]Toolbar title is not centered

I added a Toolbar in my app. 我在应用程序中添加了Toolbar
For that I am using the below xml code for custom toolbar layout. 为此,我将以下xml代码用于自定义工具栏布局。

Toolbar.xml 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. 在Android Studio输出中,工具栏标题显示在中间,如下所示。 在此处输入图片说明

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 : 编辑:我要实现的Toolbar如下,为此,请检查更新的Toolbat.xml

在此处输入图片说明

Please, help me to get the Toolbar title centered! 请帮助我使工具栏标题居中!

Try changing the textview attribute 尝试更改textview属性

android:gravity="center"

with this: 有了这个:

 android:layout_gravity="center"

You can remove nested relativelayout and add an imageview to the relativelayout lefttoolbarContainer . 您可以删除嵌套的relativelayout并将图像lefttoolbarContainer添加到relativelayout lefttoolbarContainer Since your title is inParentCenter, it shouldn't move to the right when you add the image in code(atleast in theory). 由于标题位于inParentCenter中,因此在代码中添加图像时,标题不应向右移动(理论上至少)。 ie

Toolbar.xml 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.. 希望能帮助到你..

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

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