2015-12-18 47 views
0

我有在頂部和幾個的EditText視圖的工具欄佈局:如何防止軟鍵盤只推動工具欄?

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

    <android.support.v7.widget.Toolbar 
     android:id="@+id/my_toolbar" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent"> 
    </android.support.v7.widget.Toolbar> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/my_toolbar"> 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/edit_text1"/> 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/edit_text2"/> 

      ... 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/edit_text7"/> 
    </LinearLayout> 

</RelativeLayout> 

雖然一些EditText上它們更接近屏幕的底部打開軟鍵盤,整個屏幕(包括工具欄)將被推高。除了工具欄(修復工具欄),是否有任何方法可以使軟鍵盤向上推動整個屏幕?

一些現有的answers實際上可以修復整個屏幕,但更接近底部的EditText也可能被軟鍵盤阻擋。

任何人都可以給我一個很好的解決方案嗎?

回答

2

使用ScrollView。

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

<android.support.v7.widget.Toolbar 
    android:id="@+id/my_toolbar" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent"> 
</android.support.v7.widget.Toolbar> 

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/my_toolbar"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/edit_text1"/> 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/edit_text2"/> 

      ... 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/edit_text7"/> 
    </LinearLayout> 

</ScrollView> 

+0

謝謝您的回答,這工作正常。 – Rayliu521

+0

這是行不通的仍然無法滾動意見的情況下打開鍵盤 – Erum

4

認沽機器人:fitsSystemWindows = 「真」,在第一相對佈局

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

,並在清單中添加這個

android:windowSoftInputMode="adjustPan" 
+0

感謝您的文章,我試過了,但它沒有按我的期望現在工作。也許你讓我走上正軌,我會繼續嘗試。 – Rayliu521

+0

爲我工作,謝謝! 你是否知道我是否可以將這個adjuctPan行爲變爲特定的佈局或視圖?而不是將其定義到清單中的整個應用程序。因爲有些地方我寧願使用adjustResize – BVtp

+0

您可以爲每個活動定義adjustPan,您無需爲整個應用程序定義它。 – Deepak