Android中GridView元素与Toolbar重叠问题求助
解决Toolbar与GridView重叠问题
问题根源
- FrameLayout是帧布局,所有子View默认重叠显示,不支持
layout_below这类相对布局属性,所以你给NestedScrollView加的android:layout_below="@id/toolbar"完全无效。 - 你同时给
NestedScrollView设了硬编码的layout_marginTop="60dp",又给GridView加了layout_marginTop="?android:actionBarSize",双重margin不仅冗余,还会导致布局适配性差。
解决方案
方法一:改用LinearLayout作为父布局(简单直接)
LinearLayout支持垂直方向排列,子View会自动依次摆放,无需额外margin控制位置:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <!-- Toolbar --> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/rectangle" android:title="Lingo" android:titleTextColor="@color/new_design_darker_shade"> <androidx.appcompat.widget.SearchView android:id="@+id/searchView" android:layout_marginStart="50dp" android:layout_marginEnd="40dp" android:layout_marginBottom="10dp" android:layout_marginTop="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFFF" android:queryHint="Search..." android:iconifiedByDefault="false" /> </androidx.appcompat.widget.Toolbar> <!-- NestedScrollView 填充剩余屏幕空间 --> <androidx.core.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:fillViewport="true"> <GridView android:id="@+id/mainSpecimens" android:layout_width="match_parent" android:layout_height="wrap_content" android:clipToPadding="false" android:horizontalSpacing="6dp" android:numColumns="3" android:verticalSpacing="6dp" /> </androidx.core.widget.NestedScrollView> </LinearLayout>
方法二:改用ConstraintLayout(推荐,灵活适配复杂布局)
通过约束关系精准控制View位置,适配性更强:
<?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"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/rectangle" android:title="Lingo" android:titleTextColor="@color/new_design_darker_shade" app:layout_constraintTop_toTopOf="parent"> <androidx.appcompat.widget.SearchView android:id="@+id/searchView" android:layout_marginStart="50dp" android:layout_marginEnd="40dp" android:layout_marginBottom="10dp" android:layout_marginTop="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFFF" android:queryHint="Search..." android:iconifiedByDefault="false" /> </androidx.appcompat.widget.Toolbar> <androidx.core.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="0dp" android:fillViewport="true" app:layout_constraintTop_toBottomOf="@id/toolbar" app:layout_constraintBottom_toBottomOf="parent"> <GridView android:id="@+id/mainSpecimens" android:layout_width="match_parent" android:layout_height="wrap_content" android:clipToPadding="false" android:horizontalSpacing="6dp" android:numColumns="3" android:verticalSpacing="6dp" /> </androidx.core.widget.NestedScrollView> </androidx.constraintlayout.widget.ConstraintLayout>
补充说明
两种方案都移除了冗余的marginTop设置,让布局自动适配Toolbar的实际高度,避免硬编码带来的适配问题。如果非要用FrameLayout,可将NestedScrollView的layout_marginTop改为?attr/actionBarSize,同时去掉GridView的marginTop,但这种方式灵活性差,不推荐。
内容的提问来源于stack exchange,提问作者PurpleRabbit
相关产品推荐
相关产品推荐

