选项菜单
<?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/new_folder"
android:icon="@drawable/add"
android:title="new"
app:showAsAction="never"/>
<!-- showAsAction:菜单的显示状态和时机,比如never只有在用户点击三个点的图标之后才会显示-->
<item
android:id="@+id/help"
android:icon="@drawable/help"
android:title="help"
app:showAsAction="ifRoom"/>
</menu>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.demo_menu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.new_folder:{
Toast.makeText(this,"点击了new",Toast.LENGTH_SHORT).show();
return true;
}
case R.id.help:{
Toast.makeText(this,"点击了help",Toast.LENGTH_SHORT).show();
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
上下文菜单
registerForContextMenu(textView);
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if(v.getId() == R.id.tv_show){
getMenuInflater().inflate(R.menu.demo_menu,menu);
}
}
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.new_folder:{
Toast.makeText(this,"点击了new",Toast.LENGTH_SHORT).show();
return true;
}
case R.id.help:{
Toast.makeText(this,"点击了help",Toast.LENGTH_SHORT).show();
return true;
}
default:
return super.onContextItemSelected(item);
}
}
弹出菜单
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PopupMenu popupMenu = new PopupMenu(MainActivity.this,view);
getMenuInflater().inflate(R.menu.demo_menu,popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.new_folder:{
Toast.makeText(MainActivity.this,"点击了new",Toast.LENGTH_SHORT).show();
return true;
}
case R.id.help:{
Toast.makeText(MainActivity.this,"点击了help",Toast.LENGTH_SHORT).show();
return true;
}
default:
return false;
}
}
});
popupMenu.show();
}
})
详情
应用栏(ToolBar)
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
<androidx.appcompat.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
toolbar = findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
加入回退功能
<activity
android:parentActivityName=".MainActivity"
android:name=".ChildActivity"
android:exported="false" />
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:id="@+id/child_toolbar"/>
toolbar = findViewById(R.id.child_toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar =getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
加入操作视图
![效果](https://i-blog.csdnimg.cn/blog_migrate/7082f760f850aca6d08a3b8b257acb92.png)
<item android:id="@+id/action_search"
android:title="搜索"
android:icon="@drawable/search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="androidx.appcompat.widget.SearchView" />
<item
操作视图详解