Android 自定义工具栏以 back/menu 按钮居中
Android Custom Toolbar centering with back/menu buttons
我以为这会是一个简单的任务,但是哦 Android,为什么这么难 :facepalm:
我有这个自定义工具栏布局
<android.support.v7.widget.Toolbar
....width/height stuff>
<ConstraintLayout
....width/height stuff>
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
android:gravity="center"
...start_toStartOf="parent"
...end_toEndOf="parent"
...top_toTopOf="parent"
...bottom_toBottomOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</LinearLayout>
</ContstraintLayout>
</android.support.v7.widget.Toolbar>
如果我不放置 HomeAsUpButton 或菜单项,这会正常工作。
此屏幕截图显示了当我放置后退按钮时它如何不居中
此后退按钮不是我的工具栏布局的一部分,因为我希望我的工具栏实现尽可能 'raw'。
后退按钮由
添加
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
并将我的布局向右推。
问:有什么方法可以忽略将添加到右侧的左键或菜单按钮,并使 TextView 居中?我真的必须破解它并制作自己的 Left Button/menu-like 布局吗?
如果我的问题不清楚,请告诉我,我会详细说明。
提前致谢。
尝试将您的 Toolbar
XML 代码替换为:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="@android:color/transparent"
android:elevation="1dp"
app:layout_collapseMode="pin"
app:theme="@style/toolbarTheme">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:maxLines="1"
android:textColor="@color/textPrimary"
android:textSize="@dimen/font_toolbar"
tool:text="Title" />
</android.support.v7.widget.Toolbar>
同时设置属性:
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
要删除后退按钮添加的自动添加的填充,您只需将以下属性添加到工具栏即可:
<android.support.v7.widget.Toolbar
app:contentInsetStartWithNavigation="0dp"
app:contentInsetStart="0dp"
如果你想在工具栏的中心显示 TextView
你不应该使用默认的后退按钮和菜单,因为 ToolBar
就像水平 LinnerLayout
一样,你的布局总是在后退之间按钮和菜单视图。
[[ arrow ][ your layout ][ menu items ]]
你有两种方法可以在工具栏的水平方向上显示文本:
- 当您在
ToolBar
中更改某些内容时,您必须始终找到工具栏的中间位置并手动将文本放在 middle - (text.width / 2)
的位置
- 创建自定义布局,将文本置于布局中心。
我以为这会是一个简单的任务,但是哦 Android,为什么这么难 :facepalm:
我有这个自定义工具栏布局
<android.support.v7.widget.Toolbar
....width/height stuff>
<ConstraintLayout
....width/height stuff>
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
android:gravity="center"
...start_toStartOf="parent"
...end_toEndOf="parent"
...top_toTopOf="parent"
...bottom_toBottomOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</LinearLayout>
</ContstraintLayout>
</android.support.v7.widget.Toolbar>
如果我不放置 HomeAsUpButton 或菜单项,这会正常工作。
此屏幕截图显示了当我放置后退按钮时它如何不居中
此后退按钮不是我的工具栏布局的一部分,因为我希望我的工具栏实现尽可能 'raw'。
后退按钮由
添加actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
并将我的布局向右推。
问:有什么方法可以忽略将添加到右侧的左键或菜单按钮,并使 TextView 居中?我真的必须破解它并制作自己的 Left Button/menu-like 布局吗?
如果我的问题不清楚,请告诉我,我会详细说明。
提前致谢。
尝试将您的 Toolbar
XML 代码替换为:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="@android:color/transparent"
android:elevation="1dp"
app:layout_collapseMode="pin"
app:theme="@style/toolbarTheme">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:maxLines="1"
android:textColor="@color/textPrimary"
android:textSize="@dimen/font_toolbar"
tool:text="Title" />
</android.support.v7.widget.Toolbar>
同时设置属性:
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
要删除后退按钮添加的自动添加的填充,您只需将以下属性添加到工具栏即可:
<android.support.v7.widget.Toolbar
app:contentInsetStartWithNavigation="0dp"
app:contentInsetStart="0dp"
如果你想在工具栏的中心显示 TextView
你不应该使用默认的后退按钮和菜单,因为 ToolBar
就像水平 LinnerLayout
一样,你的布局总是在后退之间按钮和菜单视图。
[[ arrow ][ your layout ][ menu items ]]
你有两种方法可以在工具栏的水平方向上显示文本:
- 当您在
ToolBar
中更改某些内容时,您必须始终找到工具栏的中间位置并手动将文本放在middle - (text.width / 2)
的位置
- 创建自定义布局,将文本置于布局中心。