使布局不可点击但儿童可点击

时间:2017-04-27 23:48:17

标签: java android android-layout android-imageview

我一直在研究系统覆盖,我发现使用WindowManager.LayoutParams x和y在屏幕上移动我的imageview很麻烦,所以我决定制作一个高度为的RelativeLayout用于保存我的imageview的设备。唯一的问题是我想让布局不可点击,以便可以点击它下面的活动,但不会在启动活动时自动查看图像视图。反正有没有这样做?这是我到目前为止的代码

 final RelativeLayout floaterLayout = new RelativeLayout(getApplicationContext());
    //this layout makes the relativelayout non clickable so that the activity below it can be clicked but it doesn't allow any children to be clicked either.
    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED + WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                    + WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE + WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN + WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL + WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSLUCENT);
    params.height = 2560;
    params.y = 0;

    floaterLayout.setBackgroundColor(Color.RED);
    floaterLayout.setAlpha(0.5f);
    floaterLayout.setLayoutParams(params);
    ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).addView(floaterLayout, params);
    ImageView view = new ImageView(getApplicationContext());
    Util.setImageDrawable(view, R.drawable.floater_dots);
    floaterLayout.addView(view);
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //do stuff
        }
    });

1 个答案:

答案 0 :(得分:0)

@Nicolas您正在创建一个未添加到父布局的相对布局。只需使用relativeLayout和imageView创建一个xml。在java代码中设置imageView的onClickListener。通过这种方式,父布局将是不可点击的,只有imageview可以点击