2016-12-24 137 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