2016-12-24 135 views
0

我不明白爲什麼它會出現在我的滾動視圖中。如果沒有編輯文本聚焦,則佈局似乎正常,如下圖所示。ScrollView被工具欄覆蓋

enter image description here

但是當用戶恰好是在編輯一個文本框,然後在工具欄和鍵盤佈局採取一切空間隱藏滾動視圖背後如下所示

enter image description here

其佈局爲:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_meal_viewer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/light_gray" 
    android:orientation="vertical"> 


    <include 
     android:id="@+id/tool_bar" 
     layout="@layout/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

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

     <android.support.v7.widget.CardView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="@dimen/activity_horizontal_margin" 
      android:background="@android:color/white"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:padding="@dimen/activity_horizontal_margin"> 


       <EditText 
        android:id="@+id/person_name" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginBottom="@dimen/activity_horizontal_margin" 
        android:background="@drawable/generic_shape_rect" 
        android:drawableLeft="@drawable/ic_people" 
        android:drawablePadding="@dimen/activity_horizontal_margin" 
        android:drawableStart="@drawable/ic_people" 
        android:gravity="center_vertical" 
        android:hint="@string/full_name" 
        android:inputType="textPersonName" 
        android:maxLines="1" 
        android:typeface="serif" /> 

       <EditText 
        android:id="@+id/person_address" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@drawable/generic_shape_rect" 
        android:drawableLeft="@drawable/ic_pin" 
        android:drawablePadding="@dimen/activity_horizontal_margin" 
        android:drawableStart="@drawable/ic_pin" 
        android:gravity="center_vertical" 
        android:hint="@string/address" 
        android:inputType="text" 
        android:maxLines="1" 
        android:typeface="serif" /> 

      </LinearLayout> 
     </android.support.v7.widget.CardView> 

     <android.support.v7.widget.CardView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="@dimen/activity_horizontal_margin" 
      android:layout_marginLeft="@dimen/activity_horizontal_margin" 
      android:layout_marginRight="@dimen/activity_horizontal_margin" 
      android:layout_marginTop="0dp" 
      android:background="@android:color/white" 
      app:cardCornerRadius="2dp"> 


      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:padding="@dimen/activity_horizontal_margin"> 

       <EditText 
        android:id="@+id/person_email" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginBottom="@dimen/activity_horizontal_margin" 
        android:background="@drawable/generic_shape_rect" 
        android:drawableLeft="@drawable/ic_email" 
        android:drawablePadding="@dimen/activity_horizontal_margin" 
        android:drawableStart="@drawable/ic_email" 
        android:ems="10" 
        android:hint="@string/email" 
        android:inputType="textEmailAddress" 
        android:maxLines="1" 
        android:typeface="serif" /> 


       <EditText 
        android:id="@+id/person_phone" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@drawable/generic_shape_rect" 
        android:drawableLeft="@drawable/ic_telephone" 
        android:drawablePadding="@dimen/activity_horizontal_margin" 
        android:drawableStart="@drawable/ic_telephone" 
        android:ems="10" 
        android:hint="@string/phone" 
        android:inputType="phone" 
        android:typeface="serif" /> 
      </LinearLayout> 


     </android.support.v7.widget.CardView> 

     <android.support.v7.widget.CardView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="@dimen/activity_horizontal_margin" 
      android:layout_marginLeft="@dimen/activity_horizontal_margin" 
      android:layout_marginRight="@dimen/activity_horizontal_margin" 
      android:layout_marginTop="0dp" 
      android:background="@android:color/white" 
      app:cardCornerRadius="2dp"> 


      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:padding="@dimen/activity_horizontal_margin"> 


       <EditText 
        android:id="@+id/person_initial_fund" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@drawable/generic_shape_rect" 
        android:drawableLeft="@drawable/ic_dollar" 
        android:drawablePadding="@dimen/activity_horizontal_margin" 
        android:drawableStart="@drawable/ic_dollar" 
        android:ems="10" 
        android:hint="@string/initial_fund" 
        android:inputType="numberSigned" 
        android:maxLines="1" 
        android:typeface="serif" /> 
      </LinearLayout> 


     </android.support.v7.widget.CardView> 

     <ImageButton 
      android:layout_width="48dp" 
      android:layout_height="48dp" 
      android:layout_gravity="center" 
      android:layout_marginBottom="@dimen/activity_horizontal_margin" 
      android:layout_marginLeft="@dimen/activity_horizontal_margin" 
      android:layout_marginRight="@dimen/activity_horizontal_margin" 
      android:layout_marginTop="0dp" 
      android:background="@drawable/selector_button_accent" 
      android:contentDescription="@string/done" 
      android:onClick="handlePeople" 
      android:src="@drawable/ic_action_done" /> 
    </LinearLayout> 
</LinearLayout> 

和工具欄佈局文件是:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:fitsSystemWindows="true" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> 

注:我也嘗試NestesScrollView但沒有工作,要麼

回答

2

默認情況下,當fitsSystemWindows上查看時,由系統提供的WindowInsetsView作爲填充消耗,這就是爲什麼你的工具欄頂部有額外的空間,該系統增加一些topPadding到當它由於透明狀態欄而接收到WindowInsets時。

但是,當鍵盤出現時,系統會以鍵盤的高度作爲底部插入引發一個新的WindowInsets。而且,由於您的工具欄會對這個新的WindowInsets做出反應,因此工具欄會將鍵盤的高度作爲底部填充添加,因此會像您的屏幕截圖一樣增長。

如果您想解決此問題,您有兩種解決方案: 首先,讓鍵盤跨越活動的內容而不是調整活動大小。您可以通過在活動的清單項設置做到這一點:

<activity 
     android:name=".YourActivity" 
     android:windowSoftInputMode="adjustPan"/> 

這將使鍵盤推的所有內容頂端確保EditText上仍可見當鍵盤了。但是當鍵盤啓動時,這可以將工具欄推到窗口的外面。這是你的電話來決定你是否可以。

如果您不確定,您可以編寫一些代碼來手動處理WindowInset,以僅將頂部填充應用於工具欄並忽略底部插入。 你可以這樣做:

ViewCompat.setOnApplyWindowInsetsListener(toolbar, new OnApplyWindowInsetsListener() { 
    @Override 
    public WindowInsetsCompat onApplyWindowInsets(
     View v, WindowInsetsCompat insets 
    ) { 
     v.setPadding(0, insets.getSystemWindowInsetTop(), 0, 0); 
     insets.consumeSystemWindowInsets(); 
     return insets; 
    } 
}); 

但要知道,即使這個代碼將編譯並在任何API級別(它使用從支持-V4的東西)上運行,這將是有效的只在API 21後,當他們改變了WindowInsets的工作方式。 如果您使用的是API 19的transparentStatusBar,則必須編寫更多代碼才能對棄用的API執行相同的操作... 我傾向於忽略這一點並允許transparentStatusBar僅從API 21開始,處理起來更麻煩說實話說得很對。

希望這會有所幫助。

1

也許你應該從谷歌支持的設計使用CoordinatorLayout代替的LinearLayout。並添加工具欄內部的AppBarLayout。 文檔閱讀this