其他分享
首页 > 其他分享> > 安卓:标题栏右上角添加按钮

安卓:标题栏右上角添加按钮

作者:互联网

话不多说,直接上代码

1.在src/res/menu目录下新建menu布局文件:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
            android:id="@+id/action_set"
            android:title="设置"
            android:orderInCategory="100"
            app:showAsAction="always"/>
</menu>

2.在activity文件中:

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);//引用menu布局文件R.menu.main
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()) {
            case R.id.action_set://监听菜单按钮
          //功能代码 Intent intent = new Intent(MainActivity.this, set.class); startActivity(intent); } return super.onOptionsItemSelected(item); }

 

标签:set,右上角,menu,安卓,标题栏,inflater,item,boolean,Override
来源: https://www.cnblogs.com/halfsaltedfish/p/10422095.html