FrameLayout 包含 ScrollView → GridView,底部固定 ImageView (圖片 A) 被 GridView 圖片遮擋、點擊不了。
FrameLayout 子 View 繪製順序 = XML 聲明順序 / addView 順序**,後聲明/添加的在上層。
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 1. 先聲明 ScrollView + GridView (底層) -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3" />
</ScrollView>
<!-- 2. 後聲明 ImageView (頂層) -->
<ImageView
android:id="@+id/floatingImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:src="@drawable/image_a"
android:clickable="true"
android:focusable="true" />
</FrameLayout>
FrameLayout container = findViewById(R.id.container);
container.addView(scrollView); // 先加底層
container.addView(floatingImage); // 後加頂層
// 或指定索引
container.addView(floatingImage, 0); // 索引 0 在最底層
container.addView(floatingImage, container.getChildCount()); // 最頂層
<ImageView
...
android:elevation="8dp" /> <!-- 明確高於 GridView (默認 0dp) -->
配合 android:translationZ 動畫效果更佳。
若 GridView 設置 android:clickable="true" 會攔截點擊。解決:
android:clickable="true" 且在頂層 (如上方案),自然接收點擊。現代佈局推薦 ConstraintLayout 扁平化層級:
<ConstraintLayout ...>
<androidx.recyclerview.widget.RecyclerView --> 替代 GridView
android:id="@+id/recyclerView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/floatingImage" />
<ImageView
android:id="@+id/floatingImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</ConstraintLayout>
優勢:無疊加順序困擾、性能更好、預覽直觀。
**FrameLayout 子 View 順序決定 Z-index**。將需置頂的 View **最後聲明/添加**,或用 elevation 顯式指定層級。新項目優先選 ConstraintLayout + RecyclerView 避免此類問題。