2010-09-06 42 views
6

Android在顯示軟鍵盤時如何確定是否要移動佈局?Android如何確定在顯示軟鍵盤時是否要移動佈局?

注:據我所知,活動屬性android:windowSoftInputMode="adjustResize|adjustResize|adjustUnspecified" 存在,這裏http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft 描述,但在我的情況下,它似乎並沒有產生任何影響。這是我的問題:

我有兩個活動,幾乎相同的佈局,但第一個是使用一個ListView,其中包含按鈕列表。第二項活動包含按鈕的滾動查看。 其餘的是一樣的,同樣的數字按鈕,元素相同的高度等

現在,當我按下搜索按鈕打開搜索輸入欄,在我的第一個活動,整個佈局被移。 在第二項活動中,佈局未向上移動,但軟鍵盤僅顯示在其上。這實際上是我希望它的行爲。我怎樣才能達到我使用ListView的活動?

在我的清單中,最初我沒有指定任何android:windowSoftInputMode屬性,但即使我這樣做,也沒有任何區別;我嘗試了所有三個值(adjustPan,adjustResize,adjustUndefined,沒有任何區別)。

這是我的佈局:

1)http://pastebin.com/5zzVxjbK

2)http://pastebin.com/KFtPuHvP

但有趣的是:當我把我的ListView的知名度在我的佈局1(左)到View.INVISIBLE,然後佈局沒有得到提升!

alt text

+0

@Mathias Lin:我想你必須發佈一些代碼。只是'listview'和'scrollview'不會做任何事情?我猜猜它取決於佈局。 – Praveen 2010-09-06 12:49:07

+0

這裏是代碼,在這兩種情況下,'button section'都被命名爲android:id =「@ + id/category_questions」;不知道這是否是相關部分。 1)http://pastebin.com/5zzVxjbK,2)http://pastebin.com/KFtPuHvP – 2010-09-06 12:59:49

+0

是的,我確定它與佈局有關,但是如何? Android開發者文檔在這方面沒有多少提及。 – 2010-09-06 13:02:30

回答

0

我可以用的RelativeLayout實現這一點,並設置android:windowSoftInputMode="adjustPan"像這樣:

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

    <include android:id="@+id/Header" layout="@layout/header" 
     android:layout_width="fill_parent" android:layout_height="wrap_content" /> 

    <ListView android:id="@+id/android:list" android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:layout_below="@id/Header" 
     android:layout_above="@+id/Footer" /> 

    <include android:id="@id/Footer" layout="@layout/footer" 
     android:layout_width="fill_parent" android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" /> 
</RelativeLayout> 

您可以通過設置頁腳比重(或layout_gravity)到「底」來實現這一目標,如果你想保留LinearLayout,但我不確定。

+0

在我的應用程序中,外部佈局是線性佈局,我確實有其他活動(即包含填充屏幕的列表視圖90%,只有一個標籤頁標題的小區域),但我沒有體驗其中的效果。我只在一些活動中看到它。我相信你的佈局的工作原理,但我想了解它背後的邏輯,所以我知道如何改變現有的LinearLayout和/或究竟是什麼導致了這種行爲。我確信有解決方法,但我不想與症狀對抗,而是想起源本身。 – 2010-09-07 04:42:20

+0

你嘗試了頁腳的重力(layout_gravity):「bottom」和「adjustPan」嗎? – 2010-09-07 04:52:34

+0

似乎ListViews被「移入視圖」,除非它們錨定在屏幕的底部,並使用adjustPan。該默認行爲可防止軟鍵盤覆蓋列表項目。 – 2010-09-07 04:54:34