ConstraintLayout 底部 activity 不可见

Bottom of ConstraintLayout activity not visible

我有布局文件:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:theme="@style/AppTheme.AppBarOverlay"
        android:fitsSystemWindows="true"
        app:liftOnScroll="true">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/topAppBar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:menu="@menu/top_app_bar_activity_pool"
            app:navigationIcon="@drawable/logo_24dp"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            style="@style/Widget.MaterialComponents.Toolbar.Primary"
            />

    </com.google.android.material.appbar.AppBarLayout>
    <include layout="@layout/content_activity_inspection_details" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

和@layout/content_activity_inspection_details:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Text"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


        <com.google.android.material.button.MaterialButton
            android:id="@+id/b_navigate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Navigate"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

在activity我有:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inspection_details);
        Toolbar toolbar = findViewById(R.id.topAppBar);
        setSupportActionBar(toolbar);

在清单中:

<activity
            android:name=".view.activity.InspectionDetailsActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar" />

问题:tv_name 不可见 - 它在工具栏下。

我可以添加 app:layout_behavior="@string/appbar_scrolling_view_behavior",即 com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior,结果是 tv_name 可见,但 button 不可见。

我应该更改什么才能让我的 ConstraintLayout 从工具栏下方开始并在屏幕底部结束?

您需要将 exitUntilCollapsed 标志添加到 AppBarLayout 标志

app:layout_scrollFlags="scroll|enterAlways|snap|exitUntilCollapsed"

并将以下行为添加到 AppBarLayout 下方的布局中,即 ConstraintLayout

app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"

因此您的布局将是:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:liftOnScroll="true">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/topAppBar"
            style="@style/Widget.MaterialComponents.Toolbar.Primary"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways|snap|exitUntilCollapsed"
            app:menu="@menu/top_app_bar_activity_pool"
            app:navigationIcon="@drawable/logo_24dp" />

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

    <include layout="@layout/content_activity_inspection_details" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

和@layout/content_activity_inspection_details:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <com.google.android.material.button.MaterialButton
        android:id="@+id/b_navigate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Navigate"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>