【问题标题】:Android CoordinatorLayout, AppBarLayout and inkluded RelativeLayout with Button OverlapAndroid CoordinatorLayout、AppBarLayout 和包含按钮重叠的 RelativeLayout
【发布时间】:2019-01-17 16:45:53
【问题描述】:

我的问题: 包含的RelativeLayout/RecyclerView 的内容不会在屏幕底部显示的按钮之前停止(见图1)。因此,RecyclerView 中的最后一个元素在滚动后被按钮重叠(见图 2)。

我目前的源码:

<?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="com.example.julian.brainy.ProjectListActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    android:fitsSystemWindows="true"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    android:theme="@style/MyActionBarTheme.AppBarOverlay">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        app:expandedTitleGravity="bottom"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:toolbarId="@+id/toolbar">

        <ImageView
            android:id="@+id/bgImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:contentDescription="@null"
            android:minHeight="100dp"
            android:scaleType="fitXY"
            android:src="@drawable/bg5"
            app:layout_collapseMode="parallax"
            app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/MyActionBarTheme.PopupOverlay"
            app:titleTextColor="@color/brainyPrimary" />

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

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

<include
    layout="@layout/content_project_list" />

<Button
    android:id="@+id/projectOverviewCreateProject"
    style="@style/Widget.AppCompat.Button.Colored"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentStart="true"
    android:layout_gravity="bottom"
    android:layout_marginBottom="60dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:stateListAnimator="@null"
    android:layout_marginTop="5dp"
    android:background="@color/brainyPrimary"
    android:text="@string/btn_create_project"
    android:textColor="@color/brainyWhite" />

<Button
    android:id="@+id/btndirectlearning"
    style="@style/Widget.AppCompat.Button.Colored"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:stateListAnimator="@null"
    android:layout_alignParentEnd="true"
    android:layout_alignParentStart="true"
    android:layout_gravity="bottom"
    android:layout_margin="5dp"

    android:background="@color/brainyPrimaryExperimental"
    android:text="@string/btn_start_timer_project_list"
    android:textColor="@color/brainyWhite" />

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

以及包含的 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
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:layout_below="@id/projectOverviewCreateProject"
android:background="@android:color/transparent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".ProjectListActivity"
tools:showIn="@layout/activity_project_list">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/projectOverviewCreateProject"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:clipToPadding="false"
    android:scrollbars="vertical" />

</RelativeLayout>

我尝试使用 ConstraintLayout,但它破坏了 AppBar。

更新:现在包含的 Layout 超出了 AppBarLayout 的顶部。

源代码更改:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        layout="@layout/content_project_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="5dp"
        app:layout_constraintBottom_toTopOf="@id/projectOverviewCreateProject"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/projectOverviewCreateProject"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:layout_margin="5dp"
        android:background="@color/brainyPrimary"
        android:stateListAnimator="@null"
        android:text="@string/btn_create_project"
        android:textColor="@color/brainyWhite"
        app:layout_constraintBottom_toTopOf="@id/btndirectlearning"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintWidth_default="spread"
        tools:layout_editor_absoluteX="5dp"
        tools:layout_editor_absoluteY="459dp" />

    <Button
        android:id="@+id/btndirectlearning"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:layout_margin="5dp"

        android:background="@color/brainyPrimaryExperimental"
        android:stateListAnimator="@null"
        android:text="@string/btn_start_timer_project_list"
        android:textColor="@color/brainyWhite"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintWidth_default="spread"
        tools:layout_editor_absoluteX="222dp" />
</android.support.constraint.ConstraintLayout>

【问题讨论】:

    标签: android xml android-recyclerview android-appbarlayout


    【解决方案1】:

    将 和 两个 Button 放在一个 ConstraintLayout 中。然后,给按钮设置正确的规则,使它们保持在布局的底部,并修改标签如下所示:

    <include
        android:layout_height="0dp"
        android:layout_width="match_parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@id/projectOverviewCreateProject"
        layout="@layout/content_project_list" />
    

    编辑: 要解决与 RecyclerView 重叠 appBar 相关的问题,只需将其添加到您的 ConstraintLayout:

    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    

    【讨论】:

    • 使用
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    • 2015-12-22
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    相关资源
    最近更新 更多