Move layouts up when soft keyboard is shown?

2019-01-02 17:43发布

I have a few elements in a RelativeView with the align bottom attribute set, when the soft keyboard comes up the elements are hidden by the soft keyboard.

I would like them to move up so that if there is enough screen space they are shown above the keyboard, or to make the section above the keyboard scrollable so the user can still see the elements.

Any ideas on how to approach this?

13条回答
君临天下
2楼-- · 2019-01-02 18:13

for Activity in its oncreate methode insert below line

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

and for fragments in its onCreate method

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
查看更多
琉璃瓶的回忆
3楼-- · 2019-01-02 18:19

similar issue i was facing with my layout which consist scroll view inside. Whenever i try to select text in EditText,Copy/Cut/Paste action bar gets moved up with below code as such it does not resize layout

android:windowSoftInputMode="adjustPan"

By modifying it to as below

1) AndroidManifest.xml

android:windowSoftInputMode="stateVisible|adjustResize"

2) style.xml file ,in activity style

<item name="android:windowActionBarOverlay">true</item>

It worked.!!!!!!!!!!!!

查看更多
永恒的永恒
4楼-- · 2019-01-02 18:21

Here is full manifest code

<activity
        android:name=".activities.MainActivity"
        android:windowSoftInputMode="adjustResize"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
查看更多
刘海飞了
5楼-- · 2019-01-02 18:23

You can also use this code in onCreate() method:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
查看更多
萌妹纸的霸气范
6楼-- · 2019-01-02 18:23

There are 3 steps to follow to scroll screen up.

  1. Write in manifest file for your activity:

android:windowSoftInputMode="adjustResize"

  1. Add following line in oncreate() method of your activity

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

  1. Then place your whole view in "Scrollview" in XML file like :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:background="@drawable/bg"
    android:layout_height="match_parent" 
    > 
    <RelativeLayout
         android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center" >
        <TextView />
         .........
        <TextView />
        <EditText >
            <requestFocus />
        </EditText>
        <Button />
    </RelativeLayout>
 </ScrollView>

P.S. Don't forget to add android:layout_gravity="center" in parent Relative layout.

查看更多
看风景的人
7楼-- · 2019-01-02 18:25

In AndroidManifest.xml, don't forget to set:

 android:windowSoftInputMode="adjustResize"

and for RelativeLayout inside ScrollView ,set :

 android:layout_gravity="center" or android:layout_gravity="bottom" 

it will be okay

查看更多
登录 后发表回答