Android xml布局:滚动时如何保持工具栏下方的视图不重叠?

时间:2018-01-08 00:00:30

标签: android xml android-layout android-coordinatorlayout

CoordinatorLayout设置中,我需要对xml做什么,以便工具栏下方的视图 - 编辑文本和回收站视图,滚动时不会与工具栏重叠?目前它看起来像这样:

enter image description here

我希望它看起来像这样:

enter image description here

我尝试从xml应该使用的方式更改CoordinatorLayout结构

CoordinatorLayout
 AppBarLayout
  CollapsingToolbarLayout
   Toolbar

为:

CoordinatorLayout
 AppBarLayout
  Toolbar
   CollapsingToolbarLayout

但是那样它根本就不滚动。

这是我的代码:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".NewContact">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="350dp"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleTextAppearance="@android:color/transparent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:titleEnabled="false"

            >

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                app:layout_collapseMode="pin"
                android:theme="@style/CustomToolbarStyle"
                />

            <LinearLayout
                android:id="@+id/holder"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="11"
                android:orientation="vertical"
                android:paddingLeft="16dp"
                android:paddingRight="16dp">

                <EditText
                    android:id="@+id/textViewCategory"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:background="@drawable/back"
                    android:hint="edit text here"
                    />

                <EditText
                    android:id="@+id/textViewName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:background="@drawable/back"
                    android:hint="edit text here"
                    />

                <EditText
                    android:id="@+id/textViewPhone"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:background="@drawable/back"
                    android:hint="edit text here"
                    />

                <EditText
                    android:id="@+id/textViewAddress"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:background="@drawable/back"
                    android:hint="edit text here"
                    />

                <EditText
                    android:id="@+id/textViewComment"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:background="@drawable/back"
                    android:hint="edit text here"
                    />

            </LinearLayout>

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


    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

4 个答案:

答案 0 :(得分:0)

首先,您需要从AppBar布局中删除EditText个孩子。您的基本布局可能如下所示。它会阻止上述情况。

<CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
  android:id="@+id/toolbar"
  android:minHeight="?attr/actionBarSize"  
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  app:titleTextColor="@android:color/white"
  android:background="?attr/colorPrimary">
</android.support.v7.widget.Toolbar>

<!-- Layout for content is here. This can be a RelativeLayout  -->

答案 1 :(得分:0)

这是我想你想要实现的目标

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

      <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme">

        <android.support.design.widget.CollapsingToolbarLayout
          android:layout_width="match_parent"
          android:layout_height="?attr/actionBarSize"
          android:fitsSystemWindows="true"
          app:contentScrim="?attr/colorPrimary"
          app:expandedTitleTextAppearance="@android:color/transparent"
          app:layout_scrollFlags="scroll|exitUntilCollapsed"
          app:titleEnabled="false">

          <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            app:layout_collapseMode="pin"
            />
        </android.support.design.widget.CollapsingToolbarLayout>
      </android.support.design.widget.AppBarLayout>
      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
          android:id="@+id/holder"
          android:layout_width="match_parent"
          android:layout_height="350dp"
          android:paddingLeft="16dp"
          android:paddingRight="16dp"
          android:orientation="vertical"
          android:weightSum="11"
          app:layout_behavior="@string/appbar_scrolling_view_behavior">

          <EditText
            android:id="@+id/textViewCategory"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@drawable/back"
            android:hint="edit text here"
            />

          <EditText
            android:id="@+id/textViewName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@drawable/back"
            android:hint="edit text here"
            />

          <EditText
            android:id="@+id/textViewPhone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@drawable/back"
            android:hint="edit text here"
            />

          <EditText
            android:id="@+id/textViewAddress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@drawable/back"
            android:hint="edit text here"
            />

          <EditText
            android:id="@+id/textViewComment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@drawable/back"
            android:hint="edit text here"
            />
        </LinearLayout>
        <android.support.v7.widget.RecyclerView
          android:id="@+id/rv"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:scrollbars="vertical"
          />
      </LinearLayout>

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

答案 2 :(得分:0)

使用AppBarLayout中的EditTexts排除LinearLayout,

在LinearLayout中包含android:layout_below =“@ + id / appbar”。

答案 3 :(得分:0)

您可以尝试这样的事情。

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="?attr/actionBarSize"
                android:orientation="vertical"
                app:layout_collapseMode="parallax">

                <View
                    android:layout_width="match_parent"
                    android:layout_height="15dp"
                    android:background="@drawable/bg_toolbar_shadow" />

                <EditText
                    android:id="@+id/search_field"
                    style="@style/TextViewNormalH1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/activity_horizontal_margin"
                    android:layout_marginRight="@dimen/activity_horizontal_margin"
                    android:layout_marginBottom="10dp"
                    android:background="@drawable/bg_rounded"
                    android:drawableLeft="@drawable/ic_search"
                    android:drawablePadding="10dp"
                    android:hint="@string/search_threads"
                    android:inputType="text"
                    android:paddingLeft="8dp"
                    android:paddingTop="12dp"
                    android:paddingRight="8dp"
                    android:paddingBottom="12dp"
                    android:textColor="#002959"
                    android:textColorHint="#7F94AC"
                    app:layout_scrollFlags="scroll|enterAlwaysCollapsed|snap" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/divider" />

                <TextView
                    android:id="@+id/text_view_header"
                    style="@style/TextViewNormalH1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#FAFAFA"
                    android:fontFamily="@font/roboto_medium"
                    android:paddingLeft="@dimen/activity_horizontal_margin"
                    android:paddingTop="10dp"
                    android:paddingRight="@dimen/activity_horizontal_margin"
                    android:paddingBottom="10dp"
                    android:text="Suggested" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/divider" />
            </LinearLayout>

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@android:color/white"
                android:minHeight="?android:attr/actionBarSize"
                app:contentInsetEndWithActions="0dp"
                app:contentInsetLeft="0dp"
                app:contentInsetStart="0dp"
                app:contentInsetStartWithNavigation="0dp"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <ImageView
                        android:id="@+id/btn_back"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="@dimen/activity_horizontal_margin"
                        android:adjustViewBounds="true"
                        android:padding="10dp"
                        android:src="@drawable/ic_back_default" />

                    <TextView
                        android:id="@+id/text_name"
                        style="@style/TextViewNormalH1"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:fontFamily="@font/roboto_regular"
                        android:gravity="center"
                        android:text="name"
                        android:textColor="#000000"
                        android:textSize="17sp" />

                </RelativeLayout>

            </androidx.appcompat.widget.Toolbar>

        </com.google.android.material.appbar.CollapsingToolbarLayout>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none"
        android:visibility="visible"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>