[英]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.