CoordinatorLayout移动内容

时间:2015-12-24 19:28:19

标签: android android-design-library android-coordinatorlayout

我有一个CoordinatorLayout,AppBarLayout,工具栏和NestedScrollview形式的主要内容及其中的内容:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
 >


   <android.support.design.widget.AppBarLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:fitsSystemWindows="true"
       >

      <android.support.v7.widget.Toolbar
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:fitsSystemWindows="true"
         >
      </android.support.v7.widget.Toolbar>
   </android.support.design.widget.AppBarLayout>

   <android.support.v4.widget.NestedScrollView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      android:fitsSystemWindows="true"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"

        />

</android.support.v4.widget.NestedScrollView>

main content is moved out

在上图中,蓝色部分是NestedScrollView(即主要内容)。如您所见,其高度的计算与工具栏无关,只是移出屏幕。

如果将CoordinatorLayout替换为任何其他布局,则NestedScrollView非常适合(同样,蓝色部分是内容,即NestedScrollView):

enter image description here

它应该如何按设计行事?如果是这样,如何使NestedScrollView完全适合剩余的屏幕而不移动它的下方部分?

更新:如果我删除NestedScrollView上的行为,它会移回到顶部,但随后会被工具栏覆盖。

更新2 :很抱歉,如果不清楚,但使用CoordinatorLayout的主要想法是能够应用各种行为,包括提供的默认行为。我有一些用户输入的文本可能不适合屏幕,所以我用NestedScrollView包围它。现在,为了便于输入文本并提供更多可用空间,我希望滚动并输入此输入时滚动工具栏(因为adjustPan和adjustResize并不理想)

3 个答案:

答案 0 :(得分:2)

尝试使用<LinearLayout>围绕它。我的意思是,在坐标布局之后。

使用LinearLayout的权重属性(如有必要)。

将Nestedscrollview的宽度和高度设置为Match_parent或fill_parent。

另一方面,您实际上并不想担心上面指定的问题。执行时应该可以正常工作。

以下是<LinearLayout>的代码。在里面使用这个标签将使我们灵活对齐。这可能很难在第一时间,但使用它并确实它会做一个技巧。

此处NestedScrollView已在屏幕内修复。

    <android.support.design.widget.CoordinatorLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    >

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

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

            <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                >

                <android.support.v7.widget.Toolbar
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fitsSystemWindows="true"
                    >
                </android.support.v7.widget.Toolbar>
            </android.support.design.widget.AppBarLayout>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/third"
            android:layout_width="fill_parent"

            android:layout_height="fill_parent"
            android:orientation="vertical">

            <android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/hello_world"

                    />

            </android.support.v4.widget.NestedScrollView>
        </LinearLayout>
    </LinearLayout>

</android.support.design.widget.CoordinatorLayout> 

答案 1 :(得分:0)

非常简单。你可以在CoordinatorLayout中试试这个。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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/parentlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity"
    android:animateLayoutChanges="true">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="@dimen/margin_16"
        app:expanded="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                >
                <!-- Your Entire Code goes Here -->


            </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

您的视图将如下所示

enter image description here

答案 2 :(得分:0)

如果有人仍然对此问题感兴趣,我想我已经解释了为什么会发生这种情况。

因此,这里的主要问题是AppbarLayout,当与CoordinatorLayout一起使用时,它可以添加滚动行为。假设您要在用户向下滚动AppbarLayout下面的视图时滚动工具栏(假设您有一个ViewPager)。然后,操作系统还需要向上滚动ViewPager来填充屏幕顶部的空间。但是如果发生这种情况,如果ViewPager的高度不足以填满整个屏幕,则底部将有一个空格。这就是为什么系统在AppbarLayout的底部添加一个额外的高度(与ViewPager完全相同的高度),以填充顶部的空间的情况,以防{{1} }滚动。

如果您以这种方式查看问题,则需要执行此操作才能获得一致的视图。因此,您必须删除AppbarLayout或决定不使用滚动行为。