1.创建menu文件,并建一个名为menu的XML文件
2.编写menuXML
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="net.zxj.hwork26.MainActivity">
<item
android:id="@+id/add_menu"
android:title="@string/add_menu">
<menu>
<item
android:id="@+id/ql_item"
android:icon="@drawable/ql"
android:title="@string/ql"
app:showAsAction="ifRoom|withText" />
<item
android:id="@+id/tj_item"
android:icon="@drawable/tj"
android:title="@string/tj"
app:showAsAction="ifRoom|withText" />
<item
android:id="@+id/sys_item"
android:icon="@drawable/sys"
android:title="@string/sys"
app:showAsAction="ifRoom|withText" />
<item
android:id="@+id/sq_item"
android:icon="@drawable/sq"
android:title="@string/sq"
app:showAsAction="ifRoom|withText" />
<item
android:id="@+id/bz_item"
android:icon="@drawable/bz"
android:title="@string/bz"
app:showAsAction="ifRoom|withText" />
</menu>
</item>
</menu>
3.添加图片到drawable资源文件
4.编写Strings.xml
<resources>
<string name="app_name">Hwork26</string>
<string name="add_menu">+</string>
<string name="ql">发起群聊</string>
<string name="tj">添加朋友</string>
<string name="sys">扫一扫</string>
<string name="sq">收钱</string>
<string name="bz">帮助与反馈</string>
</resources>
5.编写MainActivity
添加onCreateOptionsMenu方法
package net.zxj.hwork26;
import android.os.Bundle;
import android.view.Menu;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return true;
}
}
运行效果: