You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android布局中ImageView被Button遮挡如何调整使其显示在上方

问题修复方案

1. 修复ImageView被Button遮挡问题

RelativeLayout子View的绘制顺序和代码书写顺序一致,后写的控件会覆盖在先写的控件上层,同时MaterialButton默认自带1~2dp的标高,所以你当前代码里写在前面的ImageView会被Button遮挡。可以二选一修复:

  • 调整代码顺序:把ImageView的整段代码移到MaterialButton代码的下方
  • 增加标高控制:给ImageView添加属性android:elevation="3dp",标高高于Button默认值即可
    如果需要ImageView悬浮在Button的指定位置,可以额外给ImageView加相对布局约束,比如要放在Button顶部居中位置可以加:
android:layout_alignTop="@id/right"
android:layout_marginTop="-25dp"
android:layout_centerHorizontal="true"

2. 修复ViewPager与底部按钮适配问题

当前ViewPager的底部约束对齐父布局底部,和底部的按钮容器重叠,仅设置20dp的底部内边距无法适配不同屏幕尺寸和按钮高度。修复步骤:

  • 给包裹Button的RelativeLayout添加id:android:id="@+id/bottom_btn_container"
  • 将ViewPager的app:layout_constraintBottom_toBottomOf="parent"替换为app:layout_constraintBottom_toTopOf="@id/bottom_btn_container"
  • 可根据需求删除硬编码的android:paddingBottom="20dp",用约束自动适配间距即可

修改后完整代码

<?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"
    android:id="@+id/fragment"
    tools:context=".McqActivity">

    <RelativeLayout
        android:id="@+id/bottom_btn_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <com.google.android.material.button.MaterialButton
            android:id="@+id/right"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <ImageView
            android:id="@+id/starFirst"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/ic_baseline_star_24"
            android:layout_alignTop="@id/right"
            android:layout_marginTop="-25dp"
            android:layout_centerHorizontal="true"/>

    </RelativeLayout>

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/questions_view_frag"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/bottom_btn_container"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:orientation="horizontal"
        android:paddingTop="50dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

内容的提问来源于stack exchange,提问作者Karmesh Duggar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 15:54:02