【问题标题】:Adding Multiple Action Items to Multiple Toolbars in Android在 Android 中向多个工具栏添加多个操作项
【发布时间】:2015-10-26 05:51:15
【问题描述】:

到目前为止,我已经实现了以下目标:

我正在使用 Appcompat 库 来自定义我的工具栏。

正在使用的文件: 一种。 app_bar.xml(顶部工具栏) 湾。 app_bar_bottom.xml(底部工具栏)

我的 ma​​in_menu.xml 看起来像这样:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
    android:id="@+id/notices"
    android:orderInCategory="100"
    android:title="@string/notices"
    android:icon="@drawable/ic_notices_white_24dp"
    app:showAsAction="never" />
<item
    android:id="@+id/exchange_rate"
    android:orderInCategory="100"
    android:title="@string/exchange_rate"
    app:showAsAction="always"
    android:icon="@drawable/ic_rupees_white_24dp"/>

app_bar_bottom.xml 我在 MainActivity.java 中做了类似的事情:

toolbar = (Toolbar) findViewById(R.id.app_bar_bottom);
    toolbar.setLogo(R.drawable.ic_rupees_white_24dp);
    setSupportActionBar(toolbar);

ic_rupees_white_24dp是drawable文件夹下的一个图片文件。

我的问题:

一个。如何将菜单项添加到特定工具栏,即顶部和底部工具栏的不同操作项?是否可以只通过xml声明而不必使用toolbar.setLogo()等来实现?

b.有没有办法将 menu_main.xml 中声明的每个项目分配为仅设置为一个特定的工具栏?例如。上面截图中的卢比图片只作为操作项放在底部工具栏上?

请原谅我的愚蠢问题含糊不清。它只是出于我的好奇心而引起的。

谢谢。

【问题讨论】:

    标签: android xml user-interface toolbar


    【解决方案1】:

    在每个工具栏中,你应该声明它的 ActionMenuView

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/toolbar"
        android:minHeight="?attr/actionBarSize"
        android:background="@color/light_primary_color"
        >
        <android.support.v7.widget.ActionMenuView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <android.support.v7.widget.ActionMenuView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </android.support.v7.widget.Toolbar>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多