2009-12-27 76 views
84

我有一個RelativeView中的對齊底部屬性集的幾個元素,當軟鍵盤出現時,元素被軟鍵盤隱藏。顯示軟鍵盤時向上移動佈局?

我希望它們向上移動,以便如果屏幕空間足夠,它們會顯示在鍵盤上方,或者使鍵盤上方的部分可滾動,以便用戶仍然可以看到元素。

關於如何解決這個問題的任何想法?

+0

更新參考:// Android的開發者.blogspot.in/2009/04/updating-applications-for-on-screen.html) – Jayabal

+0

@Dinedal你是如何將bottom屬性對齊到你的RelativeView? – KJEjava48

回答

51

您還可以在onCreate()方法使用此代碼:

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

謝謝。它拯救了我的一天。 –

+1

對我來說,即使我設置adjustPan screnn只能移動到我點擊的編輯文本,但不是整個佈局 – murt

20

添加在AndroidManifest.xml你的活動:

android:windowSoftInputMode="adjustPan|adjustResize" 
+9

根據文檔,如果您在這裏使用兩個「調整...」標誌,行爲是未指定的。看到http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft – user1806772

15

讓你的清單文件,如活性的變化

windowSoftInputMode="adjustResize" 

OR

請在活動課中你onCreate()方法更改,如

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

我已經找到了解決自己的問題,它是和你一樣漂亮。

首先,我只有一個帶2個元素的LinearLayout,一個ImageView和一個LinearLayout,它包含2個EditText和一個Button,所以我需要讓它們在沒有鍵盤的情況下全屏分開,當鍵盤出現時它們應該看得更近。

因此,我添加了它們之間的視圖與0dp和重量1中的高度屬性,這讓我保持我的視圖彼此分離。當鍵盤出現時,因爲它們沒有固定高度,所以它們只是調整大小並保持方面。

瞧!當鍵盤存在或不存在時,佈局調整大小和我的視圖之間的距離相同。

+0

你的評論給了我另一個想法,所以+1爲你 – bvsss

+3

爲什麼不張貼代碼,爲更好的理解? – johnkarka

1

這裏充滿清單代碼

<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> 
12

類似的問題,我與我的佈局,其由內滾動視圖面對的問題。 每當我嘗試的EditText來選擇文本,複製/剪切/粘貼操作欄被移到了下面的代碼本身不調整佈局

android:windowSoftInputMode="adjustPan" 

通過將其修改爲以下

1)AndroidManifest .xml

android:windowSoftInputMode="stateVisible|adjustResize" 

2)樣式。xml文件中,動作風格

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

它的工作。!!!!!!!!!!!!

+0

Thnx解決了我的問題 – Android

+0

絕對的傳奇! –

4

我嘗試了迭戈拉米雷斯的方法,它的工作原理。 在AndroidManifest:

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

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> 
2

在其OnCreate中梅索德插入件之下線

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

活性和在其onCreate方法片段

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

在AndroidManifest.xml中,不要忘了設置:

android:windowSoftInputMode="adjustResize" 

和內部滾動型的RelativeLayout,設置:

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

一切都會好的

+0

感謝它爲我工作 –

+0

android:layout_gravity =「center」工作過,謝謝! :)現在EditText按照我想要的在鍵盤上移動 –

3

有3個步驟來向上滾動屏幕。

  1. 寫在您的活動清單文件:

機器人:windowSoftInputMode = 「adjustResize」

  • 添加以下中的OnCreate線( )您活動的方法
  • getWindow()。setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

  • 然後將在 「滾動型」 你的整個視圖中,如XML文件:
  • <?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.不要忘記在父親相對佈局中添加android:layout_gravity =「center」。

    0

    ,如果你在碎片,那麼你必須在下面的代碼添加到您的onCreate您的活動能解決問題,我

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); 
    
    從[Android開發者博客(HTTP