【问题标题】:Android: ConstraintLayout height is bigger than emulator screen's heightAndroid:ConstraintLayout 高度大于模拟器屏幕的高度
【发布时间】:2022-01-16 21:48:40
【问题描述】:

我在任何 AndroidStudio 模拟器中都遇到了 ConstraintLayout 高度问题。当我在多个设备上运行该应用程序时,我可以很好地看到所有布局渲染。但是,当我在模拟器中运行应用程序时,底部菜单超出范围。

我的 activity_main.xml 有一个带 WebView 和 BottomNavigationView 的 ConstraintLayout。这是我的模板:

template code and design image

<androidx.constraintlayout.widget.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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <WebView
        android:id="@+id/main_webview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/bottom_navigation"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/color_action_bar"
        app:elevation="16dp"
        app:itemIconTint="@color/color_iconos_bottom_bar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/main_webview" />

</androidx.constraintlayout.widget.ConstraintLayout>

当我在模拟器中运行我的应用程序时,菜单不在屏幕上。我可以看到菜单的顶部,但图标低于屏幕限制。当我点击菜单的那个小区域时,我可以导航。所以,总而言之,菜单在那里,但超出了限制。

emulator rendering image

当我在设备中运行应用程序时,我可以看到菜单完美匹配。我下载的模拟器和我拥有的设备一样的型号和SDK版本,但我还是看错了。

所以,问题在于 ConstraintLayout 的高度大于模拟器屏幕的高度。你能帮我解决这个问题吗?

【问题讨论】:

  • 你为什么有app:layout_constraintTop_toBottomOf="@id/main_webview"?为什么app:layout_constraintBottom_toBottomOf="parent" 不够?
  • match_parent 不鼓励用于 ConstraintLayout 的直接子级。您有 match_parent 的宽度,您应该使用 0dp 并将开始和结束限制为父级。进行此更改,您可能会看到更好的结果。即使它没有改变任何东西,你仍然应该做出改变。
  • 嗨@a_local_nobody。我删除了该属性,但没有任何改变。

标签: java android android-studio android-layout


【解决方案1】:

问题是您有冲突约束,您将WebView 底部链接到Navigation 顶部,并将导航顶部链接到WebView 底部。

Navigation 上的链接删除到WebView 底部。

【讨论】:

  • 嗨@Maksim Novikov。我删除了该属性,但没有任何改变。我还删除了 WebView 的顶部约束,即使没有高度,导航也位于屏幕下方。我认为问题出在ConstraintLayout的高度,因为Navigation的底部约束是ConstraintLayout的底部。
  • 是的,您需要从 Navigation 中删除 "app:layout_constraintTop_toBottomOf="@id/main_webview"" 行
【解决方案2】:

我将属性android:layout_marginBottom="45dp" 添加到BottomNavigationView 元素,现在菜单出现在模拟器中。但在设备中,菜单下方有一个黑色范围。 ConstraintLayout 的高度似乎比模拟器屏幕高度大 45dp。

【讨论】:

    猜你喜欢
    • 2014-09-19
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    • 2012-08-23
    相关资源
    最近更新 更多