Toolbar title is not centered

懵懂的女人 提交于 2019-12-04 15:15:36

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

Try changing the textview attribute

android:gravity="center"

with this:

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