How to keep background image size when software keyboard show

后端 未结 9 1684
一生所求
一生所求 2020-12-08 20:17

When the software keyboard shows, it resizes my layout and thus squeezes the background image together. My question is basically a duplicate of this question, which is expla

相关标签:
9条回答
  • 2020-12-08 20:51

    How about a solution where the 'background' is a Layout. Can we inflate a layout, make it the entire window size, then insert it as a window background?

    0 讨论(0)
  • 2020-12-08 20:53

    in Androidmanifest in activity tag use:

    <activity ... android:windowSoftInputMode="adjustPan" ></activity>
    

    check Android Developer reference

    0 讨论(0)
  • 2020-12-08 20:53

    After a lot of time searching...

    Put android:windowSoftInputMode="adjustPan|stateVisible" in each Activitys

    0 讨论(0)
  • 2020-12-08 20:57

    I was googling for a solution to this exact problem when I came across this, and after doing some research I found a solution I think is slightly better. Instead of hacking with a ImageView you can use this from your activity:

          getWindow().setBackgroundDrawableResource(R.drawable.bg_my_background);
    

    Hope it helps anyone else searching for a solution for this.

    0 讨论(0)
  • 2020-12-08 21:05

    After days of hardcore hacking I finally managed to construct a solution so advanced it might actually hurt to read it. I place an ImageView with the background behind the scrollview, and set scaleType="matrix" so it does not shrink when the keyboard is shown.

    <?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="fill_parent"
        android:background="@color/black"
        >
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/mah_kewl_background"
            android:scaleType="matrix"
            />
        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:fillViewport="true"
            >
            <RelativeLayout
                android:id="@+id/smsLayout"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                >
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="PLUNX"
                    />
            </RelativeLayout>
        </ScrollView>
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-12-08 21:05

    I can't comment on your answer but I'd like to add something.

    I understand your dilemma and why the solution you linked to isn't a complete solution for your situation (since you can't scroll to the bottom of the view). I have a similar situation with a large scrollable EditText box. When the soft keyboard pops up I don't like my background getting squished.

    I have tried your solution and while at first glance it appears to work, there are some situations where it might not be ideal:

    1. If you allow your app the have a landscape mode, the background will not resize/stretch
    2. If you run your app on a device with a larger screen or dpi, your background may not fill the screen

    Basically it seems that when you set the scaleType to matrix you're telling Android to draw the background 1:1 and not to scale. So what's happening when you open the soft keyboard using your solution is, your scrollView and imageView are all getting resized as usual, but the image src you set remains 1:1 thus continues showing the same area.

    So if for example your background image was set to a specific size (e.g. 480 x 800) and it perfectly fills your Nexus One, the moment you rotate to landscape you will have a black area along the right.

    This is moot, of course, if your background is simply a repeating pattern, in which case you can make it extremely large and hopefully compensate for various screens (maybe even tablets).

    Or you can also supply different backgrounds for different screen sizes/densities/orientations, but this still doesn't account for scaling.

    In a vain effort to try and solve this, I stumbled upon a View attribute called android:isScrollContainer. This attribute tells certain views whether or not they are allowed to resize upon display of the soft keyboard. This is also mentioned by someone in your linked solution. I have tried setting this (to false) on the ViewGroup containing my background, but any child element that scrolls seems override it causing the squishing again.

    As of right now I don't think there is a complete solution for our situation. Your's definitely works in some instances and I thank you for the effort. Hopefully Google will offer a solution in the future, but I don't think they would consider this a problem/priority.

    0 讨论(0)
提交回复
热议问题