【问题标题】:CoordinatorLayout moves contentCoordinatorLayout 移动内容
【发布时间】:2015-12-24 19:28:18
【问题描述】:

我有一个 CoordinatorLayout、AppBarLayout、Toolbar 和 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>

上图中,蓝色部分是 NestedScrollView(即主要内容)。如您所见,它的高度是在不考虑工具栏的情况下计算的,只是移出屏幕。

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

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

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

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

【问题讨论】:

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


    【解决方案1】:

    尝试用 &lt;LinearLayout&gt; 包围它。我的意思是,在坐标布局之后。

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

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


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


    这是带有 &lt;LinearLayout&gt; 的代码。在里面使用这个标签会给我们灵活的对齐方式。第一次可能很难,但使用它并练习它肯定会成功。

    这里的 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> 
    

    【讨论】:

    • LinearLayout 没有帮助,尽管这个想法很有趣。 NestedScrollView 是'match_parent'。它的内容是'wrap_content',所以它是关于它的高度。我担心它,因为我在主要内容的底部有一个视图,并且这种行为不在屏幕上。
    • 我已经遇到了同样的问题,并通过坐标和线性布局解决了它。我在上班,回家后我会把代码发给你。
    • 您的解决方案有效。唯一我不喜欢并且让人感觉像是 hack 的地方是 margin_top 明确设置为 55dp。这使得它高度依赖工具栏。
    • 我刚刚在工具栏顶部创建了另一个线性布局,它将容纳整个页面。现在它肯定会按您的预期工作
    • 在建议的布局中,工具栏在 NestedScrollView 滚动时停止滚动出屏幕。
    【解决方案2】:

    很简单。你可以在 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>
    

    您的视图将如下所示

    【讨论】:

    【解决方案3】:

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

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

    如果您以这种方式看待问题,则需要这样做才能获得一致的观点。因此,要么您必须删除 AppbarLayout,要么决定不使用滚动行为。

    【讨论】:

      猜你喜欢
      • 2015-09-18
      • 2019-03-15
      • 1970-01-01
      • 2015-12-27
      • 2015-09-15
      • 2018-06-03
      • 1970-01-01
      • 2011-08-02
      • 1970-01-01
      相关资源
      最近更新 更多