【问题标题】:Bottom of ConstraintLayout activity not visibleConstraintLayout 活动的底部不可见
【发布时间】:2020-12-12 07:50:36
【问题描述】:

我有布局文件:

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

我在活动中:

    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 从工具栏下方开始并在屏幕底部结束?

【问题讨论】:

  • 例如可以在底部添加:app:layout_constraintTop_toBottomOf="@id/tv_name"
  • @Daan 我想在屏幕底部有一个按钮。最简单的方法是添加到按钮 android:layout_marginBottom="?attr/actionBarSize" 但这是一种 hack。我确信有正确的方法来实现我想要的

标签: android android-layout android-coordinatorlayout


【解决方案1】:

您需要在 AppBarLayout 标志中添加 exitUntilCollapsed 标志

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>

【讨论】:

  • 完美!谢谢!
  • @wioskamala 很高兴听到这个消息!如果您需要进一步的帮助,请告诉我,如果符合您的查询,请接受答案..谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-20
  • 2023-03-04
  • 2015-07-03
  • 2021-07-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多