当软输入显示时,WindowManager添加的自定义视图不会调整大小

时间:2014-10-07 03:55:36

标签: android android-windowmanager

我有一个从RelativeLayout扩展的自定义View。它由ImageView,PullToRefresh视图和EditText组成。当我的应用程序收到来自GCM的推送通知时,我使用WindowManager添加自定义视图。一切正常,只有当我触摸EditText时,软键盘会显示并覆盖EditText,而不是调整其他视图的大小。我在互联网上搜索,有些人和我有同样的问题,但他们的解决方案对我没有帮助,所以我现在很困惑。请告诉我我做错了什么。

这是我的代码:

- 使用WindowManager添加视图:

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT);

ChatHeadCancelView chatheadTrashView = new ChatHeadCancelView(this, windowManager, params);

ChatHeadView chatheadView = new ChatHeadView(this, chatheadTrashView, windowManager, params, id);
chatheadView.setOnChatheadClickListener(this);

params.gravity = Gravity.TOP | Gravity.CENTER;
windowManager.addView(chatheadTrashView, params);

params.gravity = Gravity.TOP | Gravity.LEFT;
params.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
params.x = 0;
params.y = 100;
windowManager.addView(chatheadView, params);

我的自定义View构造函数的一部分:

public ChatHeadView(Context context, ChatHeadCancelView trashView,
        WindowManager windowManager, WindowManager.LayoutParams params, int id) {
    super(context);
    this.context = context;
    Activity parent = (Activity)this.context;
    // using the activity, get Window reference
    Window window = parent.getWindow();

    window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

    this.chatheadView = this;
    id = id;

    this.windowManager = windowManager;
    this.params = params;

    this.trashView = trashView;

    LayoutInflater inflater = (LayoutInflater) getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.chat_head_fragment_layout, this, true);

    // capture views
    this.primarySenderAvatar = (ImageView) findViewById(R.id.chathead_sender_user_avatar);

    this.chatView = (LinearLayout) findViewById(R.id.chat_view);

    this.chatView.setScaleX(0.0f);
    this.chatView.setScaleY(0.0f);

    ViewGroup.LayoutParams contentParams = chatView.getLayoutParams();
    contentParams.width = getDisplayWidth();
    contentParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
    chatView.setLayoutParams(contentParams);

    getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @SuppressWarnings("deprecation")
        @SuppressLint("NewApi")
        @Override
        public void onGlobalLayout() {
            chatView.setPivotX(findViewById(R.id.chathead_sender_user_avatar).getWidth() / 2);
            if (Build.VERSION.SDK_INT >= 16) {
                getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        }
    });

    this.bubbleSpringParams = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
                    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
            PixelFormat.TRANSLUCENT);
    this.bubbleSpringParams.gravity = Gravity.TOP | Gravity.LEFT;
    this.bubbleSpringParams.x = mPos[0];
    this.bubbleSpringParams.y = mPos[1];
    this.bubbleSpringParams.dimAmount = 0.6f;

    SpringSystem system;
    Spring sBubbleSpring;
    Spring sContentSpring;
    system = SpringSystem.create();
    SpringConfig springConfig = new SpringConfig(sSpringTension, sSpringFriction);

    sContentSpring = system.createSpring();
    sContentSpring.setSpringConfig(springConfig);
    sContentSpring.setCurrentValue(0.0);

    sBubbleSpring = system.createSpring();
    sBubbleSpring.setSpringConfig(springConfig);
    sBubbleSpring.setCurrentValue(1.0);

    this.mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.list_messages);
    this.postMessageBoxView = (PostMessageBoxView) findViewById(R.id.post_message_layout);
    this.topLayout = (LinearLayout) findViewById(R.id.top_layout);


    this.messageListView = (ListView) mPullToRefreshListView
            .getRefreshableView();
    this.messageListView.setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);

    this.mPullToRefreshListView.setMode(Mode.PULL_FROM_START);
    this.mPullToRefreshListView.setScrollingWhileRefreshingEnabled(false);

    this.messageListView.setAdapter(messageAdapter);
    this.messageListView.setSelection(messageAdapter.getCount() - 1);
    this.messageListView.setCacheColorHint(Color.TRANSPARENT);

    this.setListeners(id, sContentSpring, sBubbleSpring);

    this.removeTrash();

}

收到推送通知后,我会将ImageView显示为ChatHead。然后,当用户触摸它时,它将使用Spring弹出聊天视图:

private void setListeners(final int id, final Spring sContentSpring, final Spring sBubbleSpring) {

    sContentSpring.addListener(new SpringListener() {
        @Override
        public void onSpringUpdate(Spring spring) {
            float value = (float) spring.getCurrentValue();
            float clampedValue = (float) SpringUtil.clamp(value, 0.0, 1.0);
            chatView.setScaleX(value);
            chatView.setScaleY(value);
            chatView.setAlpha(clampedValue);
        }

        @Override
        public void onSpringAtRest(Spring spring) {
            chatView.setLayerType(View.LAYER_TYPE_NONE, null);
            if (spring.currentValueIsApproximately(0.0)) {
                chatView.setVisibility(View.GONE);
            }
        }

        @Override
        public void onSpringActivate(Spring spring) {
            chatView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        }

        @Override
        public void onSpringEndStateChange(Spring spring) {

        }
    });

    this.primarySenderAvatar.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            sBubbleSpring.addListener(new SpringListener() {
                @Override
                public void onSpringUpdate(Spring spring) {
                    double value = spring.getCurrentValue();
                    bubbleSpringParams.x = (int) (SpringUtil.mapValueFromRangeToRange(value, 0.0, 1.0, 0.0, mPos[0]));
                    bubbleSpringParams.y = (int) (SpringUtil.mapValueFromRangeToRange(value, 0.0, 1.0, 0.0, mPos[1]));
                    windowManager.updateViewLayout(chatheadView, bubbleSpringParams);
                    if (spring.isOvershooting() && sContentSpring.isAtRest()) {
                        sContentSpring.setEndValue(1.0);
                    }
                }

                @Override
                public void onSpringAtRest(Spring spring) {

                }

                @Override
                public void onSpringActivate(Spring spring) {

                }

                @Override
                public void onSpringEndStateChange(Spring spring) {

                }
            });

            if (onChatheadListener != null) {
                onChatheadListener.onClick(chatheadView);
            }
            chatView.setVisibility(VISIBLE);
            if (! mbExpanded) {
                getLocationOnScreen(mPos);
                mPos[1] -= 72;
                bubbleSpringParams.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
                bubbleSpringParams.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
                sBubbleSpring.setEndValue(0.0);
            } else {
                bubbleSpringParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
                bubbleSpringParams.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
                sBubbleSpring.setEndValue(1.0);
                sContentSpring.setEndValue(0.0);
            }
            mbExpanded = ! mbExpanded;
        }
    });

我知道我的代码很笨拙,我正在测试创建一个ChatHead。很感谢任何形式的帮助。谢谢你提前。

1 个答案:

答案 0 :(得分:1)

关于您的问题: 您应该替换以下代码:

params.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;

由此:

params.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;

并将您的布局放在ScrollView