2014-10-06 40 views
1

我有一個簡單的聊天活動,在頂部有一個消息輸入框,然後是帶有消息列表的滾動視圖。當軟鍵盤打開時,我希望縮小滾動視圖以便鍵盤不覆蓋最後一條消息。這是我的xml:當顯示軟鍵盤時,Scrollview不縮水

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:padding="8dp" > 

<TextView 
    android:id="@+id/chat_with" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:text="Chat with Gordon" /> 

<RelativeLayout 
    android:id="@+id/msg_container" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/chat_with" 
    android:layout_marginTop="10dp"> 

    <EditText 
     android:id="@+id/chat_message" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:hint="Type your message" /> 

    <ImageButton 
     android:id="@+id/chat_send" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:background="#00000000" 
     android:src="@+android:drawable/ic_menu_send" /> 
</RelativeLayout> 



<ScrollView 
    android:id="@+id/chat_container" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/msg_container" 
    android:layout_marginTop="15dp" 
    android:padding="10dp" 
    android:isScrollContainer="true" 
    > 

    <RelativeLayout 
     android:id="@+id/chat_text" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     > 

     <TextView 
      android:id="@+id/msg1"   
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Hi, how are you?" 
      android:layout_alignParentRight="true"/> 

     <TextView 
      android:id="@+id/msg2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="hey! All right" 
      android:layout_alignParentLeft="true" 
      android:layout_below="@+id/msg1"/> 



    </RelativeLayout> 

</ScrollView> 

</RelativeLayout> 

我只是手動插入兩個消息,但會有很多人在真正的應用程序。我已經嘗試了adjustPan | adjustResize解決方案,但它不起作用,當我觸發鍵盤時,佈局保持不變,最後的消息被鍵盤覆蓋。我怎樣才能避免這種情況? 感謝您的幫助!

+0

以及一個你應該使用S列表視圖不滾動型和兩個剛剛滾動到列表 – tyczj 2014-10-06 17:36:19

+0

結束滾動到列表的末尾不是問題,我可以通過編程來完成。問題在於鍵盤覆蓋了消息。爲什麼我不應該使用scrollview? – splinter123 2014-10-06 17:41:29

+0

閱讀一下listview的功能,找出爲什麼你應該使用它http://developer.android.com/guide/topics/ui/layout/listview.html。當鍵盤啓動時,沒有辦法顯示最後一條消息,消息可能真的很長 – tyczj 2014-10-06 17:50:30

回答

1

我通過使用清單解決了這個問題。在活動中我使用:

 android:windowSoftInputMode="stateHidden|adjustResize" 

這樣的鍵盤開始時隱藏,然後當的EditText用戶點擊鍵盤會顯示出來,並在屏幕的其餘部分被調整。

查看windowSoftInputMode以瞭解鍵盤行爲的其他設置(例如,只是掩蓋屏幕而不是調整大小)。

編輯: 要在鍵盤顯示後將滾動視圖滾動到底部,我需要稍微延遲,因爲有時滾動會先發生,然後鍵盤出現,滾動視圖不再位於底部。我結束了沒有跟上這個其他原因,但推遲滾動一點可以做到這一點:

// now scroll to the bottom 
    ScrollView sv = (ScrollView) findViewById(R.id.myScrollView); 
    sv.post(new Runnable() { 
     @Override 

     public void run() { 

      ScrollView sv = (ScrollView) findViewById(R.id.myScrollView); 
      sv.scrollTo(0, sv.getBottom()); 

     } 
    }); 
+0

很酷,謝謝!爲了使它完全工作,但我應該讓它在軟鍵盤打開時滾動到最後,但那是另一個問題。 – splinter123 2014-10-08 10:09:06

+0

@ splinter123 - 我添加了一些代碼來展示如何以編程方式滾動滾動視圖。 – Gravitoid 2014-10-08 12:48:33

+0

好的,但是什麼時候執行代碼?我想它應該放在某種監聽器中,一旦顯示軟鍵盤就會收到通知。我做了一些研究,但還沒有找到一個乾淨的解決方案。 – splinter123 2014-10-08 12:54:11