PopWindow实现显示背景窗口变暗

来源:互联网 发布:淘宝手机地址转换 编辑:程序博客网 时间:2024/05/18 03:43

popwindow的使用是非常简单的,但是怎么能跟dialog一样,显示背景自动变暗呢?

需要改变windowManager.Layoutparams.alfa 窗口透明度就好了


package com.lei.demo;import android.graphics.drawable.BitmapDrawable;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.ViewParent;import android.view.WindowManager;import android.widget.PopupWindow;import android.widget.TextView;public class MainActivity extends AppCompatActivity {    private PopupWindow pop;    private TextView showPopTv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);                setContentView(R.layout.activity_main);                showPopTv = (TextView) findViewById(R.id.show_pop_tv);        initPop();        showPopTv.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                showPop();            }        });    }    void showPop(){        changeWindowAlfa(0.7f);//改变窗口透明度        pop.showAtLocation(showPopTv, Gravity.CENTER, 0, 0);        pop.update();    }    private void initPop(){        View content = LayoutInflater.from(this).inflate(R.layout.activity_main,null);        content.setBackgroundResource(android.R.color.holo_blue_bright);        TextView tv = (TextView) content.findViewById(R.id.show_pop_tv);        tv.setText("i am a popwindow!!");        pop = new PopupWindow(content,                ViewGroup.LayoutParams.WRAP_CONTENT,                ViewGroup.LayoutParams.WRAP_CONTENT,true);//true:popupWindow获得焦点        pop.setOutsideTouchable(true);//设置外部点击可消失        pop.setBackgroundDrawable(new BitmapDrawable());//与上面一起用才能实现点击外部消失        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {            @Override            public void onDismiss() {                changeWindowAlfa(1f);//pop消失,透明度恢复            }        });    }    /*        更改屏幕窗口透明度     */    void changeWindowAlfa(float alfa){        WindowManager.LayoutParams params = getWindow().getAttributes();        params.alpha = alfa;        getWindow().setAttributes(params);    }}


效果图如下:




0 0
原创粉丝点击