当前位置: 首页 > 工具软件 > PopMenu > 使用案例 >

PopMenu

颛孙俊
2023-12-01
public class MainActivity extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener{
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void showPop(View view){
        PopupMenu popupMenu=new PopupMenu(MainActivity .this,view);
        getMenuInflater().inflate(R.menu.menu,popupMenu.getMenu());

        popupMenu.setOnMenuItemClickListener(this);
        popupMenu.show();
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_about:
                getWindow().getDecorView().setBackgroundColor(Color.RED);break;
            case R.id.action_settings:
 //设置Activity的背景颜色          getWindow().getDecorView().setBackgroundColor(Color.YELLOW);break;
        }
        return false;
    }
}

res/menu/menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="666"/>
    <item
        android:id="@+id/action_about"
        android:orderInCategory="100"

        android:title="关于"/>

</menu>

相关阅读

相关文章

相关问答