简体   繁体   English

对齐工具栏中心的文本

[英]Align text in center of toolbar

In my application I am using a Toolbar . 在我的应用程序中,我使用的是Toolbar Now I want the title(label) to be displayed in the center of the Toolbar. 现在我希望标题(标签)显示在工具栏的中心。 When the home button (back button) is not shown then the title comes in the center, but when the back button is there the text moves little further from center. 当未显示主页按钮(后退按钮)时,标题位于中央,但是当后退按钮在那里时,文本从中心移动得更远。 So how can I do this? 那我该怎么做呢?

XML XML

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

Code

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);

To use a custom title in your Toolbar all you need to do is remember is that Toolbar is just a fancy ViewGroup so you can add a custom title like so: 要在工具栏中使用自定义标题,您需要做的就是记住工具栏只是一个奇特的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>

This means that you can style the TextView however you would like because it's just a regular TextView. 这意味着您可以根据需要设置TextView的样式,因为它只是一个常规的TextView。 So in your activity you can access the title like so: 因此,在您的活动中,您可以访问标题,如下所示:

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

To center the Title in your Toolbar you must add android:layout_gravity="center" delete android:gravity="center" change android:layout_width="match_parent" to android:layout_width="wrap_content" in your TextView. 要将标题置于工具栏中心,您必须在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM