将工具栏菜单项居中对齐

3

有没有可能将工具栏中的菜单图标居中对齐?我曾经使用另一种方法,如这里所示,但那时我是在我的工具栏布局中放置图像。现在我想尝试使用菜单图标:)。

my layout

这是我的代码。

底部工具栏

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar   
 xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:appo="http://schemas.android.com/apk/res-auto"
android:minHeight="50dp"
android:background="@color/colorPrimary"
appo:theme="@style/ToolbarTheme"
appo:popupTheme="@style/Theme.AppCompat.Light">

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/news"
    app:showAsAction="always"
    android:orderInCategory="100"
    android:icon="@drawable/new_icon"
    android:title="News">

</item>

<item
    app:showAsAction="always"
    android:id="@+id/camera"
    android:icon="@drawable/camera_icon"
    android:orderInCategory="100"
    android:title="Camera"
   ></item>
<item
    app:showAsAction="always"
    android:id="@+id/facebook"
    android:icon="@drawable/facebooc_image"
    android:orderInCategory="100"
    android:title="Camera"
    >
</item>


没有一种方法可以居中OptionsMenu,您必须将其放在“工具栏”内。 - N J
3个回答

3

不要试图居中 menuItem,可以尝试使用下面的便捷实现:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:elevation="4dp"
    android:background="?attr/colorPrimary">

        // Here is the main code for  your Midle Buttons
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:layout_gravity="center"
            android:gravity="center">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_launcher"
                android:id="@+id/button1"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_launcher"
                android:id="@+id/button2"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_launcher"
                android:id="@+id/button3"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_launcher"
                android:id="@+id/button4" />
        </LinearLayout>

</android.support.v7.widget.Toolbar>

3

ToolBar是一个ViewGroup。您可以像在RelativeLayout或LinearLayout中添加图像一样将其添加到其中。例如,

<android.support.v7.widget.Toolbar
    android:id="@+id/toolBar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:layout_scrollFlags="scroll|enterAlways">

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:ellipsize="end"
        android:singleLine="true"
        android:textAllCaps="true"
        android:textColor="@color/white_color"
        android:textSize="@dimen/textSize12" />

</android.support.v7.widget.Toolbar>

2
尝试这个: 在工具栏内使用app:buttonGravity="center_vertical"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接