【问题标题】:Settings button with icon in Action Bar [duplicate]操作栏中带有图标的设置按钮[重复]
【发布时间】:2018-01-26 19:25:56
【问题描述】:

正如标题所说,我不知道如何将按钮添加到带有图标的应用标题附近的 android 操作栏。

我需要在这里添加它,如图所示 enter image description here

【问题讨论】:

    标签: java android xml android-actionbar action


    【解决方案1】:

    首先,创建一个菜单资源,随意命名。

    然后,将其添加到新的 .xml 资源中:

    <menu xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        tools:ignore="AlwaysShowAction">
        <item android:id="@+id/your_item_id"
            android:icon="@drawable/your_drawable_id"
            android:title="@string/your_string_id"
            app:showAsAction="always" />
    </menu>
    

    item标签定义了id、icon和title。 showAsAction 定义项目是否位于操作栏或子菜单中。

    建议为您的标题使用字符串资源,特别是如果您想翻译您的应用程序。

    菜单图标由您的可绘制资源定义。为了使用正确的大小,我建议使用默认的图标资源向导。

    将此添加到您的Activity 以添加菜单:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.your_menu_name, menu);
        return true;
    }
    

    为了检测菜单推送,使用这个:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.your_item_id) {
            //Do your stuff here
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    

    更多信息:https://developer.android.com/guide/topics/ui/menus.html

    【讨论】:

    猜你喜欢
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    相关资源
    最近更新 更多