Android - Center the title of the Toolbar [duplicate]

浪子不回头ぞ 提交于 2019-12-01 16:55:32

Do not use getSupportActionBar().setTitle(title); to set the title, if you have a custom Toolbar layout.

Instead, assuming your XML layout looks like this:

        <!-- Toolbar -->
        <android.support.v7.widget.Toolbar
            android:id="@+id/main_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/main_toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/main_toolbar_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>

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

You need to call, somewhere in your MainActivity's onCreate() probably:

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