android - 工具栏下方的大空间

标签 android android-layout android-toolbar

我尝试在两个具有 ConstraintLayout 的 Activity 中使用我自己的工具栏。 在一个 Activity 中,我在工具栏下方放置了一个按钮,看起来不错: image showing the good activity

然而,在另一个Activity中,我在toolbar下方放置了一个ListView,突然间Toolbar和ListView之间出现了一个很大的差距: image showing the bad activity

我应该注意,这不会出现在 AndroidStudio 的预览中。 Activity 的布局完全相同,在 Manifest 中,我将应用程序主题设置为 NoActionBar

我很高兴能得到任何帮助 :)

这是“坏” Activity 的一些代码:

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout 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"
tools:context="com.example.nutritionanalyzer.RecipeOverviewActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/recipe_overview_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    android:visibility="visible"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />

<ListView
    android:id="@+id/recipeOverviewListView"
    android:layout_width="368dp"
    android:layout_height="495dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/recipe_overview_toolbar" />

</android.support.constraint.ConstraintLayout>

也许是线

android:theme="@style/ThemeOverlay.AppCompat.ActionBar"

有罪吗?但是我在“好” Activity 中有完全相同的行......

最佳答案

只需在 ListView 中设置 android:layout_height="0dp" 因为当您在约束布局中将子项的宽度和高度设置为零以外的其他值并锚定子项的边缘时, View 将居中在 anchor 之间,因为你已经设置了这些属性(你有 anchor View 的顶部和底部边缘):

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/recipe_overview_toolbar" />

我猜你也希望 listView 匹配父级宽度,如果你想要这只是设置 android:layout_width="0dp" 因为你在 listView 中有这些属性(你有 anchor listView 水平到父级) :

app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"

并删除:

app:layout_constraintHorizontal_bias="1.0"

关于android - 工具栏下方的大空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52169965/

相关文章:

android - 主页图标向上指示器兼容操作栏

android - 我应该向 UI 开发人员询问什么图像尺寸?

android - 如何在工具栏中右对齐 MenuItem 的文本

Android 支持工具栏 colorControlNormal 颜色

java - 尝试将工具栏包含到使用 NavDrawer 模板的应用程序中

android - 通过类名启动 android Activity

适用于 Windows 10 的 Android Studio 3.4 - 找不到设备文件资源管理器

android - 使用 OnGestureListener 或 OnGesturePerformedListener 在 XML 布局之间滑动

android - 无法解析导入 android.support.design.widget.FloatingActionButton[Eclipse]

android - 数据绑定(bind)中看不到工具栏