![](/img/trans.png)
[英]How to center the title in a toolbar (even with toolbar back button displayed)
[英]How to center toolbar back button?
我在嘗試將支持工具欄上的后退按鈕居中時遇到問題。 我在 ActionBarActivity 中使用它。
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:toolbar="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
toolbar:popupTheme="@style/ThemeOverlay.AppCompat.Light"
toolbar:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
並在 Activity 的onCreate()
設置導航,如下所示:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(R.string.title_activity_scanner);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
但是,我得到的是:
如您所見,后退按鈕放錯了位置
編輯:問題似乎在於?attr/actionBarSize
actionBarSize 的自定義值設置為 40dp,但是,現在事實證明,這是標題放錯了位置。
Toolbar 支持比 ActionBar 更集中的功能集。 從頭到尾,工具欄可能包含以下可選元素的組合:
一個導航按鈕。 這可能是向上箭頭、導航菜單切換、關閉、折疊、完成或應用程序選擇的其他字形。 此按鈕應始終用於訪問工具欄容器內的其他導航目的地及其指定內容,或者以其他方式離開工具欄指定的當前上下文。 導航按鈕在工具欄的最小高度內垂直對齊(如果設置)。
因此,如果您將minHeight屬性設置為與工具欄高度 ( layout_height ) 相同,則后退箭頭將垂直居中。
如果您有非標准的工具欄高度,現在使用androidx
Toolbar
將后退按鈕 (2k20) androidx
您只需使用buttonGravity
屬性即可。
這允許您使用wrap_content
而不是特定的高度( minHeight
不允許您這樣做):
<androidx.appcompat.widget.Toolbar
app:buttonGravity="center_vertical"
android:layout_height="wrap_content"
... />
內部 ActionBarActivity
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBat().setTitle("Hello World");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
布局代碼:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/primary"
app:theme="@style/ToolbarTheme"
app:popupTheme="@style/Theme.AppCompat"/>
和風格:
<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primaryDark</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="windowActionBar">false</item>
</style>
<style name="AppTheme" parent="AppTheme.Base">
請記住,工具欄只是視圖組,因此您可以按照自己喜歡的任何方式設置樣式。 這是圖片鏈接:工具欄示例
希望能幫助到你。
實現此目的的最佳方法是從工具欄中刪除常規的后退按鈕,並在您的 XML 布局中添加一個自定義 ImageButton 並將圖像設置為它。 您可以將此 ImageButton 放置在工具欄上的任何位置。
您的活動布局代碼應該是這樣的。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<ImageButton
android:layout_width="60dp"
android:layout_height="60dp"
android:id="@+id/button"
android:src="@drawable/attach_icon"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
捎帶@Rick Sanchez 的回答- 如果您知道工具欄的minHeight
屬性的大小,則可以使用:
android:gravity="top"
app:titleMarginTop="@dimen/margin_to_center"
在工具欄中使文本居中。 如果您使用的是android:minHeight="?actionBarSize"
那么這大約是 13p
對我來說,沒有一個解決方案有效。 就我而言,問題是我申請的保證金:
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
... >
<View
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp" />
</android.support.v7.widget.Toolbar>
直接將其應用到工具欄后,按鈕垂直居中:
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
... >
<View
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
@Rick Sanchez 回答是可行的,將 Toolbar 的 layout_height 設置為與 minHeight 相同
我發現在 Toolbar 構造函數中有一個字段可以設置按鈕重力, mButtonGravity = a.getInteger(R.styleable.Toolbar_buttonGravity, Gravity.TOP);
,但v7支持Toolbar無法設置, mButtonGravity = Gravity.TOP;
讓你的按鈕大小56dp
和集scaleType
到center
與無邊距Appbar
確保你的圖標是24x24
尺寸
我正在使用
<style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">
當我應該使用
<style name="MyActionBar" parent="@style/Widget.AppCompat.Toolbar">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.