CoordinatorLayout移动内容 [英] CoordinatorLayout moves content

查看:66
本文介绍了CoordinatorLayout移动内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a CoordinatorLayout, AppBarLayout, Toolbar and main content in the form of NestedScrollview and stuff inside it:

<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(即主要内容).如您所见,它的高度是在不考虑工具栏的情况下计算出来的,只是移出了屏幕.

In the picture above, the blue section is the NestedScrollView (i.e., the main content). As you see, its height is calculated with no regard to toolbar and is just moved out of the screen.

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

If you replace the CoordinatorLayout with any other layout, the NestedScrollView fits well (again, the blue part is the content, i.e., the NestedScrollView):

设计上它应该如何表现?如果是这样,如何使NestedScrollView完全适合其余屏幕而不将其部分移到下方?

Is it how it should behave by design? If so, how to make the NestedScrollView fit the remaining screen wholly without moving its part below?

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

Update: If I remove behavior on the NestedScrollView, it moves back to top, but then gets covered by the toolbar.

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

Update 2: Sorry if it wasn't clear, but the main idea for using the CoordinatorLayout was the ability to apply various behaviors, including the default one provided. I have some user entered text that potentially may not fit into the screen, so I surround it with NestedScrollView. Now, to ease entering the text and have more space available, I'd want the toolbar to scroll out when scrolling and typing into this input (because adjustPan and adjustResize are not ideal)

推荐答案

尝试将其与<LinearLayout>包围在一起.我的意思是,在坐标布局之后.

Try surrounding it with the <LinearLayout> . I mean, after the Coordinate Layout.

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

Use weights attributes of LinearLayout ( If necessary ) .

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

Set Width and height of the Nestedscrollview as Match_parent or fill_parent.

另一件事,您实际上不想担心上面指定的问题.执行时它应该工作良好.

Another thing, you actually don't want to worry about the issue you specified above. It should work well when you execute.

这是<LinearLayout>的代码.在内部使用此标签将使我们灵活对齐.刚开始时可能会很困难,但是一定要使用它并进行练习,一定会成功的.

Here is the code with <LinearLayout> . Using this tag inside will give us flexible alignment. It may be difficult at first time , but use it and practice surely it will do a trick.

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

这篇关于CoordinatorLayout移动内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆