[英]Align text in center of toolbar
在我的应用程序中,我使用的是Toolbar
。 现在我希望标题(标签)显示在工具栏的中心。 当未显示主页按钮(后退按钮)时,标题位于中央,但是当后退按钮在那里时,文本从中心移动得更远。 那我该怎么做呢?
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_list_detail"
style="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/green">
<TextView
android:id="@+id/tv_ld_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello"
android:gravity="center"
android:textColor="#fff"
android:textSize="@dimen/textSize"
android:textStyle="bold" />
</android.support.v7.widget.Toolbar>
toolbar = (Toolbar) findViewById(R.id.toolbar_list_detail);
setSupportActionBar(toolbar);
tvHeader = (TextView) toolbar.findViewById(R.id.tv_ld_header);
//rlToolbar = (RelativeLayout) toolbar.findViewById(R.id.rl_toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
final Drawable upArrow = getResources().getDrawable(R.drawable.back);
getSupportActionBar().setHomeAsUpIndicator(upArrow);
要在工具栏中使用自定义标题,您需要做的就是记住工具栏只是一个奇特的ViewGroup,因此您可以像这样添加自定义标题:
<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);
要将标题置于工具栏中心,您必须在TextView中添加android:layout_gravity =“center”delete android:gravity =“center”将android:layout_width =“match_parent”更改为android:layout_width =“wrap_content”。
<TextView
android:id="@+id/tv_ld_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:layout_gravity="center"
android:textColor="#fff"
android:textSize="@dimen/textSize"
android:textStyle="bold" />`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.