Android布局异常:TextView与Button被ImageView遮挡,调整组件顺序可恢复
问题原因与解决方案
核心原因
你使用的是垂直方向的LinearLayout,且给它设置了固定高度200dp。原布局里ImageView被放在最后,它的高度为wrap_content(由图片实际尺寸决定),加上前面几个TextView和Button的高度,总内容高度超过了父布局的200dp。LinearLayout是按代码声明顺序依次排列控件的,当总高度超出父容器时,后面的控件会优先占据剩余空间,前面的控件会被挤出父容器范围,视觉上就像是被ImageView遮挡了。
解决方案
方案1:调整控件顺序(最直接)
把ImageView移到LinearLayout的最前面,让它先分配空间,后面的TextView和Button在剩余空间内排列,就能正常显示在ImageView下方,和你调整组件树后的效果一致。修改后的布局代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="200dp" android:paddingBottom="20dp" android:background="@color/yellow" android:orientation="vertical"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="centerCrop" app:srcCompat="@drawable/book_1" /> <TextView android:id="@+id/textView4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> <TextView android:id="@+id/textView5" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> <TextView android:id="@+id/textView6" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>
方案2:用权重分配空间(保留原顺序)
如果不想调整控件顺序,可以给ImageView设置layout_weight,让它填充剩余空间,避免挤压前面的控件。修改ImageView的属性:
<ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:scaleType="centerCrop" app:srcCompat="@drawable/book_1" />
这样ImageView会占据TextView和Button之外的所有剩余空间,前面的控件就能正常显示。
内容的提问来源于stack exchange,提问作者RGB5
相关产品推荐
相关产品推荐

