【发布时间】:2015-07-01 06:30:59
【问题描述】:
我问了一个类似的问题here... 我在答案中有一些教程。 但这个问题是不同的。因为这些方法都不适用于我的项目。
我希望工具栏中的所有图标居中
我测试了所有可用的方式...但图标总是浮动在左侧。
我希望图标居中(我更喜欢左图标向左浮动,右图标向右浮动,其他图标浮动居中)
Activity.Main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" // I add vertical
tools:context=".MainActivity">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent" // I add match_parent
android:layout_gravity="center" /> // I add center
</LinearLayout>.
也在 tool_bar.xml 中
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:background="@color/ColorPrimary"
android:elevation="2dp"
android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"
android:layout_gravity="center" // I add center
xmlns:android="http://schemas.android.com/apk/res/android" />
在 main_menu.xml
中<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item
android:id="@+id/action_forward"
android:title="forward"
android:icon="@drawable/ic_action_arrow_forward"
app:showAsAction="always"
></item>
<item
android:id="@+id/action_zoom_in"
android:title="zoom in"
android:icon="@drawable/ic_action_zoom_in"
app:showAsAction="always"
></item>
<item ...
对于<item> 中的上述代码(main_menu.xml),我尝试了所有这些代码:
android:layout_width="match_parent"
android:layout_weight=".2" // 20%
android:gravity="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
我真的不知道该怎么做才能对齐图标。 我用谷歌搜索了几个小时,我尝试了所有的 senario
我怎么了? 对于所有测试,我看不到任何变化...
之前我想要 pic2(在上图中)但现在我只想居中!
对于我的所有测试,我只得到图片 1。没有任何变化。
【问题讨论】:
-
实际上,您并没有将所有项目都居中对齐。一个是左对齐,一个是右对齐,4 个是居中对齐。所以我的建议是不要在
Toolbar中使用菜单,而是使用RelativeLayout。 -
为什么不使用自定义布局来代替工具栏?
标签: android android-layout android-toolbar android-menu