使用 ConstraintLayout 查看与工具栏的重叠
View overlapping with Toolbar using ConstraintLayout
我遇到了使用 ConstraintLayout
的 ListView 与兼容工具栏重叠的问题
我想要做的就是有一个工具栏,然后在 space 的其余部分有一个列表视图。很简单
这是我的布局
<android.support.constraint.ConstraintLayout 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.support.v7.widget.Toolbar
android:id="@+id/app_toolbar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Toolbar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:visibility="visible"
app:layout_constraintTop_toTopOf="parent"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/app_toolbar2"
/>
</android.support.constraint.ConstraintLayout>
我不确定问题是否是 ListView 的 layout_height
优先于其他所有内容,因此要使 ListView 与父级一样高,确保底部存在重叠或截断,因为工具栏拿了一些 space。
让 ListView 占据工具栏后剩余的垂直 space 的正确方法是什么?
将 app:layout_behavior="@string/appbar_scrolling_view_behavior"
添加到您的 ListView 以设置滚动和间距行为。它适用于 CoordinatorLayout 或 RelativeLayout,可能它也适用于 ConstraintLayout。
试试这个,应该有效:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="56dp"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>
<android.support.constraint.ConstraintLayout
android:id="@+id/main_area"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<ListView
android:id="@+id/listview"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
根据 ConstraintLayout 的官方文档:
- 您需要使用
MATCH_CONSTRAINT
(0dp) 而不是 MATCH_PARENT
Important: MATCH_PARENT is not recommended for widgets contained in a
ConstraintLayout. Similar behavior can be defined by using
MATCH_CONSTRAINT with the corresponding left/right or top/bottom
constraints being set to "parent".
只需更改 Listview 宽度和高度 = "0dp"
<ListView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/app_toolbar2"/>
使用下面的方法添加 Toolbar
- <android.support.design.widget.CoordinatorLayout> or ConstraintLayout
-<android.support.design.widget.AppBarLayout>
- <android.support.v7.widget.Toolbar>
- </android.support.v7.widget.Toolbar>
- </android.support.design.widget.AppBarLayout>
-<LinearLayout
app:layout_behavior="@string/appbar_scrolling_view_behavior">
-<ListView>
-</Listview>
-</LinearLayout>
-</android.support.design.widget.CoordinatorLayout> or ConstraintLayout
基本上,Framelayout 试图与其他视图重叠,这就是它发生的原因,请尝试将 framelayout 放在其他视图组中
干杯
试试这个..应该行得通。
在约束布局中使用此代码。
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="@+id/container"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar" />
我遇到了使用 ConstraintLayout
的 ListView 与兼容工具栏重叠的问题
我想要做的就是有一个工具栏,然后在 space 的其余部分有一个列表视图。很简单
这是我的布局
<android.support.constraint.ConstraintLayout 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.support.v7.widget.Toolbar
android:id="@+id/app_toolbar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Toolbar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:visibility="visible"
app:layout_constraintTop_toTopOf="parent"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/app_toolbar2"
/>
</android.support.constraint.ConstraintLayout>
我不确定问题是否是 ListView 的 layout_height
优先于其他所有内容,因此要使 ListView 与父级一样高,确保底部存在重叠或截断,因为工具栏拿了一些 space。
让 ListView 占据工具栏后剩余的垂直 space 的正确方法是什么?
将 app:layout_behavior="@string/appbar_scrolling_view_behavior"
添加到您的 ListView 以设置滚动和间距行为。它适用于 CoordinatorLayout 或 RelativeLayout,可能它也适用于 ConstraintLayout。
试试这个,应该有效:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="56dp"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>
<android.support.constraint.ConstraintLayout
android:id="@+id/main_area"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<ListView
android:id="@+id/listview"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
根据 ConstraintLayout 的官方文档:
- 您需要使用
MATCH_CONSTRAINT
(0dp) 而不是 MATCH_PARENT
Important: MATCH_PARENT is not recommended for widgets contained in a ConstraintLayout. Similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent".
只需更改 Listview 宽度和高度 = "0dp"
<ListView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/app_toolbar2"/>
使用下面的方法添加 Toolbar
- <android.support.design.widget.CoordinatorLayout> or ConstraintLayout
-<android.support.design.widget.AppBarLayout>
- <android.support.v7.widget.Toolbar>
- </android.support.v7.widget.Toolbar>
- </android.support.design.widget.AppBarLayout>
-<LinearLayout
app:layout_behavior="@string/appbar_scrolling_view_behavior">
-<ListView>
-</Listview>
-</LinearLayout>
-</android.support.design.widget.CoordinatorLayout> or ConstraintLayout
基本上,Framelayout 试图与其他视图重叠,这就是它发生的原因,请尝试将 framelayout 放在其他视图组中
干杯
试试这个..应该行得通。 在约束布局中使用此代码。
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="@+id/container"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar" />