Android, Position Menu Items in Toolbar Layout

二次信任 提交于 2019-12-11 01:01:01

问题


I have two toolbars (top and bottom), as shown:

The two toolbars are defined in their own xml files, nothing special:

top_toolbar.xlm

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/ColorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

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

bottom_toolbar.xlm

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/ColorBottomTrans"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

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

And added to the main activity:

activity_main.xlm

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/relLayout"
    android:background="@android:color/white">

    <include
        android:id="@+id/tool_bar"
        layout="@layout/tool_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"></include>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World"
        android:layout_below="@+id/tool_bar"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_margin="20dp" />

    <include
        android:id="@+id/tool_bar_bottom"
        layout="@layout/tool_bar_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"></include>

</RelativeLayout>

Where the items in each toolbar are inflated from menu files.

MainActivity

private void initToolbars() {
    topToolbar = (Toolbar) findViewById(R.id.tool_bar);
    bottomToolbar = (Toolbar) findViewById(R.id.tool_bar_bottom);

    setSupportActionBar(topToolbar);

    bottomToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.action_item_1:
                    break;
            }
            return true;
        }
    });
    bottomToolbar.inflateMenu(R.menu.menu_main);
    bottomToolbar.getBackground().setAlpha(125);
}

But what I would like to do is move "Item 1" in the bottom toolbar to the left side, as shown:

Is there a way to do this?

Thanks in advance.


回答1:


The easiest solution I came up with was the to change the bottom toolbar layout:

bottom_toolbar.xml

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/ColorBottomTrans"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lin"
        android:orientation="horizontal"
        android:layout_gravity="left"
        android:gravity="center_vertical">

        <ImageView
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:src="@mipmap/ic_launcher"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ITEM 1"
            android:textStyle="bold"
            android:textSize="12dp"
            android:id="@+id/toolbar_title"
            android:layout_marginLeft="10dp"
            android:textColor="@android:color/white" />
    </LinearLayout>

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

And then when I initialize the toolbar, to add:

LinearLayout linearLayout = (LinearLayout) bottomToolbar.findViewById(R.id.lin);
linearLayout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        ... do stuff ...
    }
});

Just need to remember to remove "Item 1" from the bottom_toolbar_menu.xml. And can create a drawable file to visually show click events.



来源:https://stackoverflow.com/questions/33702196/android-position-menu-items-in-toolbar-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!