【发布时间】: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>
当我在模拟器中运行我的应用程序时,菜单不在屏幕上。我可以看到菜单的顶部,但图标低于屏幕限制。当我点击菜单的那个小区域时,我可以导航。所以,总而言之,菜单在那里,但超出了限制。
当我在设备中运行应用程序时,我可以看到菜单完美匹配。我下载的模拟器和我拥有的设备一样的型号和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