How to prevent Soft Keyboard from resizing background image

前端 未结 12 1762
耶瑟儿~
耶瑟儿~ 2020-12-30 00:09

I have a typical listview with an edittext and a button at the bottom of the activity.

When I click onto the edittext, the sof

相关标签:
12条回答
  • 2020-12-30 00:46

    There is no way to prevent the soft keyboard from resizing a background image.

    Use this scaleType(matrix) and prepare a suitable image.

    <?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:id="@+id/RelativeLayoutchat"
      >
    
      <LinearLayout
        android:orientation="horizontal"
        android:gravity ="clip_horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true">
    
       <ImageView
            android:gravity ="clip_horizontal"
            android:id="@+id/chat_bak_img"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="matrix">
       </ImageView>
    
       </LinearLayout>
    
      <LinearLayout  android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical">
    
      <ListView
            android:id="@+id/chat"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:transcriptMode="alwaysScroll"
            android:divider="#000000"
            android:clickable="false"
            android:layout_weight="9"
            android:cacheColorHint="#00000000"/>
    
    
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_weight="1"
            style="@android:style/ButtonBar"
            android:gravity="center"
            android:paddingLeft="3dip"
            android:paddingTop="3dip"
            android:paddingRight="3dip">
            <EditText
                android:id="@+id/chatMsg"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:maxLines="5"
                android:enabled="true"
                android:textSize="17sp"
                android:maxLength="150"/>
            <Button
                android:id="@+id/sendMsg"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/send_withSpace"
                android:layout_weight="4"/>
        </LinearLayout>
    
      </LinearLayout>
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-12-30 00:48

    I have the same problem with imageview resize while keyboard up i have solved with manifest android:windowSoftInputMode="adjustPan" and following is the layout example in my case

    i.e frg_write_social:

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frg_writeyourpost_ll_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:fitsSystemWindows="true"
    android:isScrollContainer="false"
    android:scrollbars="none" >
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    
        <RelativeLayout
            android:id="@+id/frg_writeyourpost_rl_top"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/frg_writeyourpost_ll_bottum"
            android:layout_alignParentTop="true"
            android:minHeight="400dip" >
    
            <ImageView
                android:id="@+id/frg_writyourpost_iv"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerInParent="true"
                android:adjustViewBounds="true"
                android:scaleType="fitXY" />
        </RelativeLayout>
    
        <LinearLayout
            android:id="@+id/frg_writeyourpost_ll_bottum"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="vertical"
            android:padding="10dp" >
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="10dp"
                android:background="@android:color/white"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="10dp" >
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:gravity="center"
                    android:text="Write your post"
                    android:textColor="#727F8D"
                    android:textSize="10sp" />
    
                <com.example.views.CustomEditText
                    android:id="@+id/frg_writeyourpost_et_message"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/slide_image"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:background="@null"
                    android:inputType="textMultiLine"
                    android:maxHeight="@dimen/slide_image"
                    android:maxLines="3"
                    android:textSize="11sp" >
                </com.example.views.CustomEditText>
    
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:orientation="horizontal" >
    
                    <TextView
                            android:id="@+id/frg_writeyourpost_tv_totalcharacter"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="0"
                        android:textColor="#727F8D"
                        android:textSize="8sp" />
    
                    <TextView
                     android:id="@+id/frg_writeyourpost_tv_characterLeft"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:gravity="center"
                        android:text="character"
                        android:textColor="#727F8D"
                        android:textSize="8sp" />
                </LinearLayout>
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="horizontal" >
    
                <RadioGroup
                    android:id="@+id/frg_writepost_fbtw_rggroup"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" >
    
                    <RadioButton
                        android:id="@+id/frg_writepost_rb_facebook"
                        android:layout_width="0dip"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@drawable/fb_check_selector"
                        android:button="@android:color/transparent"
                        android:gravity="center"
                        android:padding="10dip"
                        android:text="@string/post_for_facebook"/>
    
                    <View
                        android:layout_width="5dip"
                        android:layout_height="match_parent"
                        android:background="@android:color/transparent"/>
    
                    <RadioButton
                        android:id="@+id/frg_writepost_rb_twitter"
                        android:layout_width="0dip"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@drawable/tw_check_selector"
                        android:button="@android:color/transparent"
                        android:gravity="center"
                        android:padding="10dip"
                        android:text="@string/post_for_twitter"/>
                </RadioGroup>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
    

    if there is possible to load image dynamically you can resize image with picaaso while load in imageview

    0 讨论(0)
  • 2020-12-30 00:48

    I read a lot of answers over StackOverflow and took some from many to make my app works:

    1. No ScrollView at all in my activity_main.xml

    2. In AndroidManifest.xml added this attribute to the

      <activity android:name=".MainActivity" ...>

    tag:

    android:windowSoftInputMode="adjustResize"
    
    1. In MainActivity.java inside onCreate() I added getWindow().setBackgroundDrawableResource(R.drawable.app_bg);

    It works for me when all the other solutions I've tried didn't. If you got up to here, I hope it'll work for you too.

    0 讨论(0)
  • 2020-12-30 00:49

    Use android:windowSoftInputMode="adjustResize" and simply wrap your background ImageView in a ScrollView.

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="none"
        android:overScrollMode="never">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/> 
    </ScrollView>
    
    0 讨论(0)
  • 2020-12-30 00:50

    I have tried so may solutions for solving this problem. android:windowSoftInputMode="adjustPan" will make your whole screen shift with keyboard. Generally we have a title of the screen on the top. Using this flag it will also go out of the visible area, which make bad user experience. I use android:windowSoftInputMode="adjustResize" This will resize the whole screen but it will cause the same problem @Maurice state in the question.

    So here is my final solution:

    In Manifest

    android:windowSoftInputMode="adjustResize|stateAlwaysHidden"
    

    In XMl

    Dont Set any background here.And keep your view under ScrollView

    In Java

    You need to set the background to window:

    getWindow().setBackgroundDrawableResource(R.drawable.bg_wood) ;
    
    0 讨论(0)
  • 2020-12-30 00:52

    all you need to do is to set the background gravity to the top

    <item android:drawable="@drawable/background_image" android:gravity="top"/>
    

    Here is tutorial

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