How to prevent lifting listview when keyboard goes up

回眸只為那壹抹淺笑 提交于 2019-12-12 04:12:12

问题


This is code MainActivity java and main.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:gravity="center"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#75F575">

<LinearLayout
    android:clickable="true"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:layout_width="250dp"
    android:layout_height="100dp"
    android:background="#C69817"
    android:id="@+id/secondLayout">

    <ListView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/mainListView1"/>

</LinearLayout>

</LinearLayout>

Java:

      public void onCreate(Bundle       savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);              
            ListView lv = (ListView) findViewById(R.id.mainListView1);
            ArrayAdapter<String> a = new ArrayAdapter<String>(this, 
                    android.R.layout.simple_list_item_1 ,
                    new String [] {"item1","item2"});
            lv.setAdapter(a);
            getWindow().setSoftInputMode(
                    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}   

When keyboard is shown, listview lifting up.How to prevent this. Without listview - all normal(layout NOT go up). Thank you.

UPDATE QUESTION with answer to FOliveira. Unfortunately i can't remove java code (setSoftInputMode) in my real app. Java code must be and layout with listview must NOT GO UP. Try to remove listview, and you will see the layout not moving absolutely, Why layout with listview is moving? How prevent this according my conditions?


回答1:


I'm using android:windowSoftInputMode="stateVisible|adjustNothing" , but this only for API higher 10(unfortunately).

Update: After adding to listview this attributes:

android:focusableInTouchMode="false"
android:isScrollContainer="false"

I'm completely solved my problem.




回答2:


You need to add android:windowSoftInputMode="adjustPan" to your tag in the AndroidManifest.xml file.

The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.

If this option does not fit your needs, you can allways check Android documention about soft input mode



来源:https://stackoverflow.com/questions/23199252/how-to-prevent-lifting-listview-when-keyboard-goes-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!