BottomSheetDialog 清除默认样式

该文章介绍了如何在Android开发中自定义BottomSheetDialog,通过设置Gravity、透明背景和去除内边距,使其达到设计图的全屏无边距效果。代码示例展示了如何创建一个全屏显示并具有特定样式的DeleteImageDialog。
摘要由CSDN通过智能技术生成

序言

BottomSheetDialog 默认样式有各种边距。于是选了一个简单的dialog。记录一下。怎么去除默认样式。达到设计图的标准。

设计图

在这里插入图片描述

代码

DeleteImageDialog

public class DeleteImageDialog extends BottomSheetDialog {


    public DeleteImageDialog(@NonNull Context context) {
        super(context);
    }

    CallBack<Void> callBack;

    public void setCallBack(CallBack<Void> callBack) {
        this.callBack = callBack;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.delete_image_dialog);
        fullScreen();
        initViews();
    }

    private void initViews() {
        findViewById(R.id.tv_cancel).setOnClickListener(v->{
            dismiss();
        });
        findViewById(R.id.tv_delete).setOnClickListener(v->{
            if(callBack!=null){
                callBack.call(null);
            }
            dismiss();
        });
    }


    void fullScreen() {

        //设置在底部显示
        getWindow().setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        //清除decorView的背景色
        getWindow().getDecorView().setBackground(new ColorDrawable(Color.TRANSPARENT));
        getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundColor(Color.TRANSPARENT);

        //清除decorView的padding
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        getWindow().getAttributes().width = ViewGroup.LayoutParams.MATCH_PARENT;
        //在小米机型中,必须把高度设置为MATCH_PARENT,否则会抖动。
        getWindow().getAttributes().height = ViewGroup.LayoutParams.MATCH_PARENT;


    }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    tools:background="#333"
    android:layout_height="match_parent"
    tools:ignore="MissingDefaultResource">

    <RelativeLayout
        android:layout_gravity="bottom"
        android:background="@drawable/delete_image_dialog_bottom_bg"
        android:layout_width="match_parent"
        android:layout_height="156dp">
        <TextView
            android:id="@+id/tv_delete"
            android:layout_marginTop="38dp"
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="删除图片"
            android:textColor="#fffd2d3b"
            android:textSize="16sp"
            />
        <View
            android:layout_marginTop="71dp"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="15dp"
            android:layout_width="match_parent"
            android:background="#F4F4F4"
            android:layout_height="1dp"/>
        <TextView
            android:id="@+id/tv_cancel"
            android:background="@drawable/delete_image_dialog_title_bg"
            android:gravity="center"
            android:textSize="16sp"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="15dp"
            android:layout_marginBottom="25.5dp"
            android:text="取消"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="44dp"
            tools:ignore="HardcodedText" />
    </RelativeLayout>
</FrameLayout>

背景

delete_image_dialog_bottom_bg

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/white" />
    <corners
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp" />
</shape>

delete_image_dialog_title_bg

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:width="345dp" android:height="44dp">
        <shape android:shape="rectangle">
            <solid android:color="#fff7f7f7" />
            <corners android:radius="22dp" />
        </shape>
    </item>
</selector>

实际效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值