如何在片段的屏幕中心对齐动作栏标题?

时间:2017-02-23 08:47:45

标签: android android-fragments fragment

我的操作栏有菜单,只在片段中设置标题。

无法对齐中心。

我该怎么办?默认只显示左侧。如何在片段标题中的操作栏上的CENTER位置对齐?

1 个答案:

答案 0 :(得分:0)

您必须使用工具栏。

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="@color/action_bar_bkgnd"
app:theme="@style/ToolBarTheme" >


 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Toolbar Title"
    android:layout_gravity="center"
    android:id="@+id/toolbar_title" />


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

这意味着您可以根据需要设置TextView的样式,因为它只是一个常规的TextView。因此,在您的活动中,您可以像这样访问标题:

Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title);