在操作栏的右上方添加一个按钮 [英] Add a button to the top right of action bar

查看:81
本文介绍了在操作栏的右上方添加一个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法像我的默认设置Button一样在ActionBar的右上角添加一个按钮?我删除了设置Button,但是我想在该位置添加自定义Button.

Is there a way I can add a button to the top right of my ActionBar, like where the default settings Button is? I removed the settings Button but I'd like to add a custom Button in it's place.

推荐答案

您可以通过编辑/创建菜单xml文件来添加按钮:

You can add a button by editing/create the menu xml file:

<?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/action_name"
        android:icon="@drawable/you_resource_here"
        android:title="Text to be seen by user"
        app:showAsAction="always"
        android:orderInCategory="0"/>

</menu>

然后在您的活动中,如果您创建了一个新文件,则需要编辑onCreateOptionsMenu

Then in your activity, if you created a new file your need to edit onCreateOptionsMenu

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

,您可以通过以下方法编辑操作:

and you can edit what the actions do in the following method:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_name) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

这篇关于在操作栏的右上方添加一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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