Androidstudio menu bar settings

Create a new menu column in the res resource, and create a main.xml file in the menu file. The code is as follows: If you don't know how to create it, the following are the specific steps:

First, create a new menu folder in the lower directory
, right-click the res directory—New--Directory, enter the folder name
menu, and click OK. Then create a new menu file named main under this folder, right-click on the menu folder and a
New-Menu resource file

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/发起群聊_item"

    android:title="发起群聊"

    />

    <item
        android:id="@+id/添加朋友_item"
        android:title="添加朋友"
        />
    <item
        android:id="@+id/扫一扫_item"
        android:title="扫一扫"
        />
    <item
        android:id="@+id/收付款_item"
        android:title="收付款"
        />


</menu>

This is the preview of the preview

Maybe you are not satisfied with the simple operation of just displaying the menu bar, then add click on the submenu of the menu bar to make simple function feedback.

Add the corresponding code zuizhongzz in MainActivity.java

public boolean onOptionsItemSelected (MenuItem item) {
        switch (item.getItemId()) {
            case R.id.发起群聊_item:
                Toast.makeText(this, "You clicked 发起群聊按钮", Toast.LENGTH_SHORT).show();
                break;
            case R.id.添加朋友_item:
                Toast.makeText(this,
                        "You clicked 添加朋友按钮", Toast.LENGTH_SHORT).show();
            case R.id.扫一扫_item:
                Toast.makeText(this, "You clicked 扫一扫按钮", Toast.LENGTH_SHORT).show();
                break;
            case R.id.收付款_item:
                Toast.makeText(this,
                        "You clicked 收付款按钮", Toast.LENGTH_SHORT).show();

                break;
            default:
        }
        return true;
    }

 Final renderings:

 

 

Guess you like

Origin blog.csdn.net/Abtxr/article/details/124052724