中心的工具栏的菜单项

时间:2016-05-16 08:24:41

标签: android menu toolbar

是否可以将工具栏的菜单图标与中心对齐?我使用了here显示的另一种方式,但是我将图像放在我的工具栏布局中。现在我想尝试使用菜单图标:)。

my layout

这是我的代码。

Bottm工具栏

<?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>

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分:2)

ToolBar只是ViewGroup。你可以将Images添加到它中,就像在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 :(得分:0)

尝试一下: app:buttonGravity="center_vertical"在工具栏内

相关问题