android - 无法使用 DrawerLayout 将图像放在 ListView 上方
问题描述
我想使用约束布局来制作我的应用程序,但我的侧边导航菜单只能与 DrawerLayout 一起使用,所以现在我无法将元素放在我想要的位置。
即使图像设置为“wrap_content”,它也是全屏的并且在我的列表前面,我希望它位于列表上方。
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<ListView
android:id="@+id/lvLista"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/titleDates"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#B5D5B5"
app:layout_constraintTop_toBottomOf="@+id/titleDates" />
<ImageView
android:id="@+id/titleDates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
app:srcCompat="@drawable/dias_de_coleta" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_menu" />
</android.support.v4.widget.DrawerLayout>
解决方案
您必须将 ListView 和 ImageView 放在它自己的 ViewGroup 中,因为 DrawerLayout 不支持任何视图定位约束。您可以将其视为 FrameLayout。
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/titleDates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf=parent"
app:srcCompat="@drawable/dias_de_coleta" />
<ListView
android:id="@+id/lvLista"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/titleDates"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#B5D5B5" />
</android.support.constraint.ConstraintLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_menu" />
</android.support.v4.widget.DrawerLayout>
另外: layout_alignParentTop和layout_below仅在容器是 RelativeLayout 时才有效。
并且layout_constraintTop_toBottomOf仅在容器是 ConstraintLayout 时才有效。
如果您的视图不是这些视图组的子视图,则使用它们毫无用处。
推荐阅读
- vba - 访问 25%、50%、75% 四分位数(来自查询和结果目标的数据是报告)
- c# - 如何避免 MediatR 请求处理程序中的代码重复?
- javascript - .Net core - Ajax post 不将变量传递给控制器方法
- java - IntelliJ 可能以错误的方式索引:编译器工作,但 IDEA 不喜欢 Main 方法
- go - Prometheus 客户端过早清理计数器?
- django - Django-按字段排序,除非它为空,然后使用另一个
- database - 如何消除结果中的重复项
- excel - VBA 代码不会遍历工作表以删除定义的行
- java - 扫描仪和 ArrayList Java
- html - 使用 flexbox 设置等高列