如何在桌面上显示自定义View

1.设置权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>



2.必须写个类它里面有静态属性WindowMangaer 和 WindowManager.LayoutParams
public class MyManager {
    private static WindowManager mWindowManager;
    private static WindowManager.LayoutParams params;


    public static void createView(Context context){
        WindowManager windowManager = getWindowManager(context);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.type = WindowManager.LayoutParams.TYPE_PHONE;//必须加上它不能会报错
        params.gravity = Gravity.CENTER;
        params.format = PixelFormat.RGBA_8888;//这最后加上它然显示会周围出现类似马赛克
        params.width = 300;
        params.height =200;
        final View tempView = LayoutInflater.from(context).inflate(R.layout.my_view,null);
        tempView.setLayoutParams(params);
        tempView.findViewById(R.id.miss).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mWindowManager.removeViewImmediate(tempView);
            }
        });
        windowManager.addView(tempView,params);


    }


    private static WindowManager getWindowManager(Context context){
        if(mWindowManager == null){
            mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }
        return mWindowManager;
    }


3.在Activity中调用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值