微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 在操作栏的右上角添加一个按钮

有没有办法可以在ActionBar的右上角添加一个按钮,就像认设置按钮的位置一样?我删除了设置按钮,但我想在其中添加一个自定义按钮.

解决方法

您可以通过编辑/创建菜单xml文件添加按钮:
<?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

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

您可以编辑以下方法执行的操作:

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

原文地址:https://www.jb51.cc/android/312036.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐