梦里花落0921
这只是为了帮助连接所有的片段@MrEngineer 13答案与@Jonik和@RickSanchez评论与正确的顺序,以帮助实现标题为中心容易!布局TextAppearance.AppCompat.Widget.ActionBar.Title :
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_gravity="center" />
用正确的顺序实现的方法: Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
setSupportActionBar(toolbar);
mTitle.setText(toolbar.getTitle());
getSupportActionBar().setDisplayShowTitleEnabled(false);请别忘了投票@MrEngineer 13答案 !!!下面是一个示例项目ToolbarCenterTitleSample希望能帮助别人;)