Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 协调布局移动内容_Android_Android Design Library_Android Coordinatorlayout - Fatal编程技术网

Android 协调布局移动内容

Android 协调布局移动内容,android,android-design-library,android-coordinatorlayout,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

我有一个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>

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

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

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

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


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

尝试用
环绕它。我是说,在坐标布局之后

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

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


另外,您实际上不想担心上面指定的问题。当你执行的时候,它应该工作得很好


这是带有
的代码。在内部使用此标签将为我们提供灵活的对齐方式。第一次可能很难,但使用它并练习肯定会奏效

这里嵌套的滚动视图固定在屏幕内部。

    <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> 

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


如果还有人对这个问题感兴趣,我想我可以解释为什么会发生这种情况

因此这里的主要问题是
AppbarLayout
,以及它与
协调布局一起使用时添加滚动行为的能力。假设当用户在
AppbarLayout
下方的视图上向下滚动时,您希望滚动工具栏(假设您有一个
ViewPager
)。然后,操作系统还需要向上滚动
ViewPager
,以填充屏幕顶部的空间。但是,如果
ViewPager
没有足够的高度填满整个屏幕,那么底部就会有一个空间。这就是为什么系统会在
ViewPager
的底部添加一个额外的高度(与
AppbarLayout
的高度完全相同),以便在
AppbarLayout
滚动时填充顶部的空间


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

LinearLayout没有帮助,尽管这个想法很有趣。NestedScrollView是“匹配父项”。它的内容是“包裹内容”,所以它是关于它的高度。我担心这一点,因为我在主要内容的底部有一个视图,并且它不在屏幕上。我已经有了同样的问题,并用坐标和线性布局解决了它。我在工作,我一到家就把密码发给你。你的解决方案行得通。唯一一件我不喜欢的事情,让我感觉像是一个黑客,那就是把margin_top显式设置为55dp。这使得它高度依赖于工具栏。我刚刚在工具栏顶部创建了另一个线性布局,它将容纳整个页面。现在,它肯定会像您预期的那样工作在建议的布局中,工具栏不会在NestedScrollView滚动上从屏幕上滚动出来。我想避免这种外观-蓝色边框意味着内容隐藏在底部导航栏后面,并适合可见部分。我明白您的意思,您应该选中此项,或者可以按照Android布局结构文档中的说明透明导航栏