Android布局问题:BottomNavigationView被ScrollView遮挡
问题根源
- 约束属性完全无效:你用
RelativeLayout做根布局,却给BottomNavigationView和FrameLayout加了app:layout_constraintXXX系列属性——这些是ConstraintLayout专属的,在RelativeLayout里根本不生效,等于白写。 - ScrollView遮挡导航栏:
ScrollView高度设为match_parent会占满整个屏幕,而且RelativeLayout里后添加的控件层级更高,ScrollView写在导航栏后面,自然就把它挡住了。 - 导航图标不显示:
bottom_nav_menu.xml里用了tools:icon,这只是编辑器预览用的,运行时不会生效;还错误加了app:actionProviderClass,直接导致图标无法正常展示。
修复后的activity_main.xml
把根布局换成ConstraintLayout,正确配置控件约束关系:
<?xml version="1.0" encoding="utf-8"?> <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" tools:context=".MainActivity"> <!-- 滚动内容区域,底部约束到导航栏顶部 --> <ScrollView android:layout_width="match_parent" android:layout_height="0dp" android:padding="16dp" app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <com.denzcoskun.imageslider.ImageSlider android:id="@+id/image_slider" android:layout_width="wrap_content" android:layout_height="300dp" app:iss_auto_cycle="true" app:iss_delay="1000" app:iss_period="1000" app:iss_text_align="CENTER" /> <!-- Title --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:paddingBottom="16dp" android:text="Museums and Culture of Wales" android:textSize="24sp" android:textStyle="bold" /> <!-- National Museum Cardiff --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:lineSpacingExtra="4dp" android:text="1. National Museum Cardiff: Located in the capital city, this museum houses an incredible collection of art, archaeology, and natural history. It’s a must-visit for anyone interested in exploring the artistic and scientific achievements of Wales." android:textSize="16sp" /> <ImageView android:layout_width="match_parent" android:layout_height="200dp" android:layout_marginTop="8dp" android:layout_marginBottom="16dp" android:scaleType="centerCrop" android:src="@drawable/img1" /> <!-- St Fagans National Museum of History --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:lineSpacingExtra="4dp" android:text="2. St Fagans National Museum of History: A unique open-air museum near Cardiff that features over forty original buildings from different historical periods in Welsh history. It provides a vivid picture of life in Wales over centuries." android:textSize="16sp" /> <ImageView android:layout_width="match_parent" android:layout_height="200dp" android:layout_marginTop="8dp" android:layout_marginBottom="16dp" android:scaleType="centerCrop" android:src="@drawable/img2" /> <!-- Big Pit National Coal Museum --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:lineSpacingExtra="4dp" android:text="3. Big Pit National Coal Museum: This UNESCO World Heritage Site in Blaenavon lets you explore the life of coal miners with a guided underground tour. It’s a fascinating way to learn about Wales' industrial heritage." android:textSize="16sp" /> <ImageView android:layout_width="match_parent" android:layout_height="200dp" android:layout_marginTop="8dp" android:layout_marginBottom="16dp" android:scaleType="centerCrop" android:src="@drawable/img3" /> <!-- Pontcysyllte Aqueduct --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:lineSpacingExtra="4dp" android:text="4. Pontcysyllte Aqueduct: Not a museum but a cultural icon, this aqueduct is a masterpiece of engineering and a UNESCO World Heritage Site. It’s a testament to Wales’ contributions to industrial innovation." android:textSize="16sp" /> <ImageView android:layout_width="match_parent" android:layout_height="200dp" android:layout_marginTop="8dp" android:layout_marginBottom="16dp" android:scaleType="centerCrop" android:src="@drawable/img4" /> <!-- Closing Paragraph --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:lineSpacingExtra="4dp" android:text="Wales is also celebrated for its music, language, and festivals. The Eisteddfod, an annual cultural festival, showcases the best in Welsh poetry, music, and art, while the Welsh language (Cymraeg) remains an integral part of the country's identity. Whether you’re visiting its museums or experiencing its vibrant culture firsthand, Wales offers an unforgettable journey into the past and present." android:textSize="16sp" /> </LinearLayout> </ScrollView> <!-- 底部导航栏,固定在屏幕底部 --> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottomNavigationView" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="visible" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:menu="@menu/bottom_nav_menu" /> </androidx.constraintlayout.widget.ConstraintLayout>
修复后的bottom_nav_menu.xml
去掉错误的actionProviderClass,把tools:icon换成app:icon,确保运行时图标正常显示:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/home" android:title="Home" app:icon="@drawable/ic_home" /> <item android:id="@+id/maps" android:title="Maps" app:icon="@drawable/ic_maps" /> <item android:id="@+id/details" android:title="Details" app:icon="@drawable/ic_detail" /> <item android:id="@+id/settings" android:title="Settings" app:icon="@drawable/ic_setting" /> </menu>
关键改动说明
- 根布局替换为
ConstraintLayout,让约束属性真正生效; - 设置
ScrollView高度为0dp,通过约束让它占据导航栏以上的所有空间,彻底避免遮挡; - 修复菜单文件的图标配置,去掉无用属性,确保运行时能正常展示图标。
内容的提问来源于stack exchange,提问作者DamianZ98
相关产品推荐
相关产品推荐

