ImageView resizes when keyboard open

2019-03-03 10:01发布

问题:

This is my code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" >

<ImageView
    android:id="@+id/bgImage"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:contentDescription="@string/todo"
    android:scaleType="fitXY"
    android:src="@drawable/default_wallpaper" />

<LinearLayout
    android:id="@+id/mainLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/title"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/in"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        android:cacheColorHint="@android:color/transparent"
        android:divider="@android:color/transparent"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/edit_text_out"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_gravity="bottom"
            android:layout_weight="1"
            android:inputType="none" />

        <Button
            android:id="@+id/button_send"
            android:layout_width="70dp"
            android:layout_height="fill_parent"
            android:text="@string/send"
            android:textSize="15sp"
            android:textStyle="bold" />
    </LinearLayout>
</LinearLayout>

</RelativeLayout>

In this when I tap on EditText soft keyboard opens but my ImageView is shrinking.

I used android:windowSoftInputMode="adjustPan" in manifest file but by using this it will push up whole activity upper side but I want ImageView as it is and when keyboard is open ImageView is cut from bottom not from top as in WhatsApp application.

Any help will appreciate. Thank you ..

回答1:

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.



回答2:

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



回答3:

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