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:26

I tried a method of Diego Ramírez, it works. In AndroidManifest:

    <activity
        android:name=".MainActivity"
        android:windowSoftInputMode="stateHidden|adjustResize">
        ...
    </activity>

In activity_main.xml:

<LinearLayout ...
    android:orientation="vertical">

<Space
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="1" />

<EditText
    android:id="@+id/edName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/first_name"
    android:inputType="textPersonName" />

<EditText ...>
...
</LinearLayout>
查看更多
千与千寻千般痛.
3楼-- · 2019-01-02 18:32

Add in AndroidManifest.xml for your activity:

android:windowSoftInputMode="adjustPan|adjustResize"
查看更多
看淡一切
4楼-- · 2019-01-02 18:34

if you are in fragments then you have to add the below code to your onCreate on your activity it solves issue for me

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
查看更多
路过你的时光
5楼-- · 2019-01-02 18:34

Alright this is very late however I've discovered that if you add a List View under your edit text then the keyboard will move all layouts under that edittext without moving the ListView

<EditText
android:id="@+id/et"
android:layout_height="match_parent"
android:layout_width="match_parent"



/>

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"//can make as 1dp
    android:layout_below="@+id/et" // set to below editext
    android:id="@+id/view"
    >
**<any layout**

This is the only solution that worked for me.

查看更多
唯独是你
6楼-- · 2019-01-02 18:35

Yes, check out this article on the Android developers' site which describes how the framework handles the soft keyboard appearing.

The android:windowSoftInputMode attribute can be used to specify what happens on a per-activity basis: whether the layout is resized or whether it scrolls etc.

查看更多
余生无你
7楼-- · 2019-01-02 18:35

Make changes in the activity of your Manifest file like

windowSoftInputMode="adjustResize"

OR

Make changes in your onCreate() method in the activity class like

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
查看更多
登录 后发表回答