Android - Soft Keyboard pushes layout of my activi

2019-02-07 20:11发布

问题:

My activity's layout is as shown below.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical">

   <FrameLayout android:id="@+id/title_bar"
      android:layout_width="fill_parent"
      android:layout_height="25dip"
      android:background="@drawable/bg_title" />

   <LinearLayout android:id="@+id/main"
      android:width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_weight="1">
         <ListView android:id="@+id/android:list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
         <TextView android:id="@+id/android:empty"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
      </FrameLayout>
   </LinearLayout>

   <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="50dip" >
      <EditText android:id="@+id/query"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content" 
         android:hint="Enter some search terms"
         android:singleLine="true" 
         android:layout_weight="1" />
      <Button android:id="@+id/btn_hide"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:background="@drawable/btn_hide"
         android:layout_marginLeft="6dip" />
   </LinearLayout>
 </LinearLayout>

So, the search box is fixed to the bottom of the screen.

But, when user clicks the EditText, Soft Keyboard shows up and pushes the layout out of the screen except the search box.

I'm just starting out with Android, so am I doing anything wrong here??

回答1:

Try adding the following for your activity in Manifest:

android:windowSoftInputMode="adjustPan"


回答2:

For those that are interested, the difference between android:windowSoftInputMode="adjustPan" and android:windowSoftInputMode="adjustResize" :

"adjustResize"

The activity's window is resized to make room for the soft keyboard on screen.

"adjustPan"

The contents of the activity's window are automatically panned so that the current focus is never blocked by the keyboard. This is so the user can 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.

Android Documentation

Cited link



回答3:

@Raj If you are working with a tabbed application you have to add

android:windowSoftInputMode="adjustPan"

on the Activity where you are adding the tabs, this would most probably be your launcher activity.

Here is a snippet from my code

<activity
   android:label="@string/app_name"
   android:name=".MainActivity" android:windowSoftInputMode="adjustPan">
   <intent-filter >
     <action android:name="android.intent.action.MAIN" />
     <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>