PopupWindow弹出框和自定义Menu

PopupWindow 弹出框

###思路
在这里插入图片描述

直接看代码

布局文件

在这里插入图片描述

另外一个popup.xml文件可以随意设置

Activity文件

ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
imageView=findViewById(R.id.plus);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupMenu();
}
});
}

private void popupMenu() {
    PopupWindow popupWindow = new PopupWindow(this);
    View view = View.inflate(this, R.layout.popup, null);
    //设置视图![在这里插入图片描述](https://img-blog.csdnimg.cn/20190510214304301.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzkxNTkyMQ==,size_16,color_FFFFFF,t_70)
    popupWindow.setContentView(view);
    popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
    popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    popupWindow.setOutsideTouchable(true);
    popupWindow.showAsDropDown(imageView);
    popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            WindowManager.LayoutParams attributes =getWindow().getAttributes();
            attributes.alpha=1.0f;
            getWindow().setAttributes(attributes);
        }
    });
    WindowManager.LayoutParams attributes =getWindow().getAttributes();
    attributes.alpha=0.5f;
    getWindow().setAttributes(attributes);
}

运行效果

在这里插入图片描述

点击外部会把菜单取消

自定义Menu

布局文件

这是在menu布局文件中的item
在这里插入图片描述
layout布局文件
在这里插入图片描述

Activity文件

在需要自定义文件中重写onCreateOptionsMenu()方法;

public boolean onCreateOptionsMenu(Menu menu) {

//获取视图,注意这里是getMenulnflater 不能用View;
getMenuInflater().inflate(R.menu.items,menu);
//获取Menu中itme布局文件中的id
MenuItem item = menu.findItem(R.id.screen);
MenuItem item2 = menu.findItem(R.id.back);
MenuItem item3 = menu.findItem(R.id.raking);
MenuItem item4 = menu.findItem(R.id.treasure);
//设置每一个itme中item中的视图
item.setActionView(R.layout.tem);
item2.setActionView(R.layout.tem);
item3.setActionView(R.layout.tem);
item4.setActionView(R.layout.tem);

    ImageView vi = item.getActionView().findViewById(R.id.item_image);
    TextView vv = item.getActionView().findViewById(R.id.item_wen);
    
    ImageView v2 = item2.getActionView().findViewById(R.id.item_image);
    TextView vv2 = item2.getActionView().findViewById(R.id.item_wen);
    
    ImageView imageView3 = item3.getActionView().findViewById(R.id.item_image);
    TextView textView = item3.getActionView().findViewById(R.id.item_wen);
    
    ImageView imageView4 = item4.getActionView().findViewById(R.id.item_image);
    TextView textView2 = item4.getActionView().findViewById(R.id.item_wen);
    
    vi.setImageResource(R.drawable.screen);
    vv.setText("筛选");
    v2.setImageResource(R.drawable.back);
    vv2.setText("返回");
    imageView3.setImageResource(R.drawable.raking);
    textView.setText("排行榜");
    imageView4.setImageResource(R.drawable.teasure);
    textView2.setText("财富");
    return super.onCreateOptionsMenu(menu);
}

效果图

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值