ImageView resizes when keyboard open

て烟熏妆下的殇ゞ 提交于 2019-12-02 09:27:51

I had a similar issue in which a custom View - ParallaxBackground - that I've adapted was supposed to act as a background image with a parallax effect driven by the scroll of a ViewPager in the same activity.

In other words, I have a View that displays an image with a width slightly larger than the screen's and when the user scrolls along the ViewPager, the image in the ParallaxBackground scrolls a little as well.

Now in my case, I had some EditTexts inside my ViewPager's Fragments and when the user clicked on them, causing the soft keyboard to show up, the ParallaxBackground (and consequently, the background image) would be re-sized and squeezed "against my will".

So I added a boolean isBackground, two ints for fixedWidth and fixedHeight and overrode my View's onMeasure() like so:

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {


    if(isBackground) 
    // If this is being used as a background and is supposed to occupy the whole screen...
    {
                    // force the View to have the specified dimensions
        setMeasuredDimension(fixedWidth, fixedHeight);
    }
    // ...aply default measures...
    else
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

 }

I know this is not a "beautiful" solution but, even though my answer is not 100% related to your problem, you might want to try extending the ImageView class and overriding onMeasure.

Then, in your onCreate method you can set isBackground to true and give fixedWidth and fixedHeight the values for your measured screen width and height.

remove the parent layout (the RelativeLayout) and make the LinearLayout containing the dialog box only, the parent! plus, add android:windowSoftInputMode="stateVisible|adjustResize" to the AndroidManifest.xml

CodeForMe

Add background image from Java not from XML:

getActivity().getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable.splash_image));

In manifest add

android:windowSoftInputMode="adjustResize|stateHidden"

The state hidden can written as per requirement if you have to hide the keyboard on opening of screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!