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

Android BottomSheetDialog图片溢出及背景异常问题求助

BottomSheetDialog布局异常及图片边框透明问题

在使用BottomSheetDialog时,遇到以下问题:想将图片放置在BottomSheetDialog的CardView上方,该效果在Android Studio设计编辑器中正常显示,但在设备上出现异常。

问题效果图

效果图说明

  • 左侧:Android Studio设计编辑器(显示正常)
  • 中间:设备运行结果(card_view:cardElevation="0dp"不生效,存在白色背景)
  • 右侧:Figma中的预期效果

需求:移除白色背景,让图片正确显示在顶部。已解决部分问题,现需实现图片边框透明效果。

XML布局代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    android:id="@+id/fragment_character_menu_bottom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/tools">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="72dp"
            app:cardBackgroundColor="@color/secondary"
            card_view:cardElevation="0dp"
            card_view:cardUseCompatPadding="false">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="bottom"
                        android:layout_marginStart="140dp"
                        android:layout_marginBottom="16dp"
                        android:layout_marginTop="20dp"
                        android:orientation="vertical">
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Lorem ipsum"
                            android:textColor="@color/secondary_font"
                            android:layout_marginStart="8dp"
                            app:layout_constraintBottom_toTopOf="@+id/frameLayout"
                            app:layout_constraintStart_toEndOf="@+id/imageView" />

                        <FrameLayout
                            android:id="@+id/frameLayout"
                            android:layout_width="match_parent"
                            android:layout_height="2dp"
                            android:layout_marginTop="4dp"
                            android:layout_marginEnd="32dp"
                            android:alpha="0.05"
                            android:background="@color/white"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintStart_toEndOf="@+id/imageView" />
                    </LinearLayout>

                <LinearLayout
                    android:id="@+id/fragment_character_bottom_sheet_rename"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:clickable="true"
                    android:focusable="true"
                    android:orientation="horizontal"
                    android:paddingStart="32dp"
                    android:padding="8dp">
                    <ImageView
                        android:layout_width="32dp"
                        android:layout_height="32dp"
                        app:srcCompat="@drawable/ic_edit_line"
                        />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:textColor="@color/secondary_font"
                        android:layout_marginLeft="16dp"
                        android:text="Rename"/>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/fragment_character_bottom_sheet_edit"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:clickable="true"
                    android:focusable="true"
                    android:orientation="horizontal"
                    android:paddingStart="32dp"
                    android:padding="8dp">
                    <ImageView
                        android:layout_width="32dp"
                        android:layout_height="32dp"
                        app:srcCompat="@drawable/ic_round_edit"
                        />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginLeft="16dp"
                        android:textColor="@color/secondary_font"
                        android:text="Edit"/>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/fragment_character_bottom_sheet_share"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:clickable="true"
                    android:focusable="true"
                    android:orientation="horizontal"
                    android:paddingStart="32dp"
                    android:padding="8dp">
                    <ImageView
                        android:layout_width="32dp"
                        android:layout_height="32dp"
                        app:srcCompat="@drawable/ic_share"
                        />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:textColor="@color/secondary_font"
                        android:layout_marginLeft="16dp"
                        android:text="Share"/>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/fragment_character_bottom_sheet_delete"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:clickable="true"
                    android:focusable="true"
                    android:orientation="horizontal"
                    android:paddingStart="32dp"
                    android:padding="8dp">
                    <ImageView
                        android:layout_width="32dp"
                        android:layout_height="32dp"
                        app:srcCompat="@drawable/ic_trashcan_solid"
                        />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:textColor="@color/secondary_font"
                        android:layout_marginLeft="16dp"
                        android:text="Delete"/>
                </LinearLayout>
            </LinearLayout>
        </androidx.cardview.widget.CardView>
        <com.google.android.material.imageview.ShapeableImageView
            android:id="@+id/imageView"
            android:layout_width="128dp"
            android:layout_height="128dp"
            android:layout_margin="8dp"
            app:strokeWidth="4dp"
            app:srcCompat="@mipmap/ic_launcher"
            app:shapeAppearanceOverlay="@style/ImageView.Corner.Round"
            />
    </RelativeLayout>


</RelativeLayout>

实现代码

BottomSheetDialog mBottomSheetDialog = new BottomSheetDialog(mMainActivity);
View sheetView = mMainActivity.getLayoutInflater().inflate(R.layout.fragment_character_bottom_sheet, null);
mBottomSheetDialog.setContentView(sheetView);
mBottomSheetDialog.show();

感谢提供的所有建议和思路。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 15:30:23