Android,工具栏布局中的位置菜单项 [英] Android, Position Menu Items in Toolbar Layout

查看:108
本文介绍了Android,工具栏布局中的位置菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个工具栏(顶部和底部),如下所示:

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

这两个工具栏是在各自的xml文件中定义的,没什么特别的:

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>

并添加到主要活动中:

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);
}

但是我想做的是将底部工具栏中的项目1"移到左侧,如图所示:

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?

谢谢.

推荐答案

我想到的最简单的解决方案是更改底部工具栏的布局:

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 ...
    }
});

只需记住要从 bottom_toolbar_menu.xml 中删除项目1".并且可以创建一个可绘制的文件以直观地显示点击事件.

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.

这篇关于Android,工具栏布局中的位置菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆