问题描述:

Is it possible to align the menu icons of a toolbar in the center? I used another way shown here,but there I was putting images inside my Toolbar layout. Now I want to try it with menu icons:).

here is my code.

bottm toolbar

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

网友答案:

ToolBar is just a ViewGroup. You can add Images into to it just like into a RelativeLayout or LinearLayout. for example,

<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>
网友答案:

Instead of trying to center menuItem , you can try the below handy implementation :

<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>
相关阅读:
Top