BottomSheetDialog的基本使用

最近在开发一个商城的项目,在商品详情内需要弹框选择商品的SKU,如图:
这里写图片描述

商品图片需要凸出到背景阴影去,于是最新想到使用dialog从底部弹出,选择使用BottomSheetDialog

首先在build.gradle下引入:

 implementation 'com.android.support:design:26.1.0'

第二:xml布局文件 dialog_goods_sku_layout:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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="wrap_content">

    <RelativeLayout
        android:id="@+id/top_layout"
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:elevation="1dp"
        tools:ignore="UnusedAttribute">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="75dp"
            android:background="@color/colorWhite"
            android:layout_alignParentBottom="true"
            android:paddingStart="120dp">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:paddingBottom="5dp"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tv_total_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="¥88.88"
                    android:textColor="@color/colorPrimary"
                    android:textSize="16sp"
                    android:textStyle="bold"
                    tools:ignore="HardcodedText,TextViewEdits" />


                <TextView
                    android:id="@+id/tv_goods_no"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:visibility="visible"
                    android:text="商品编码:888888"
                    tools:ignore="HardcodedText" />

            </LinearLayout>


        </FrameLayout>

        <android.support.v7.widget.CardView
            android:id="@+id/cardView"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_marginEnd="14dp"
            android:layout_marginStart="20dp"
            android:layout_marginBottom="5dp"
            app:cardBackgroundColor="@color/colorWhite"
            app:cardCornerRadius="6dp"
            app:cardElevation="2dp">

            <ImageView
                android:id="@+id/iv_goods"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:src="@mipmap/ic_test"
                tools:ignore="ContentDescription" />

        </android.support.v7.widget.CardView>

    </RelativeLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/top_layout"
        android:background="@color/colorWhite">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="14dp"
            android:layout_marginStart="14dp"
            android:layout_marginTop="100dp"
            android:orientation="vertical">

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

    <LinearLayout
        android:id="@+id/bottom_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/scrollView"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_add_shopping_sku"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:alpha="0.8"
            android:background="@color/colorAccent"
            android:gravity="center"
            android:text="加入购物车"
            android:textColor="@color/colorWhite"
            tools:ignore="HardcodedText" />

        <TextView
            android:id="@+id/tv_orders_sku"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/colorPrimary"
            android:gravity="center"
            android:text="立即购买"
            android:textColor="@color/colorWhite"
            tools:ignore="HardcodedText" />
    </LinearLayout>

</RelativeLayout>

第三:代码上初始化

  private void initDialog() {
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this);
        bottomSheetDialog.setContentView(R.layout.dialog_goods_sku_layout);
        bottomSheetDialog.show();
    }

以上三步操作就可以实现从底部弹出dialog对话框,是不是觉得很简单,但此时会发现和我们需求有点区别,没有达到图片凸出到阴影透明区,如图:
这里写图片描述

出现以上问题,在代码上给BottomSheetDialog 设置一个透明的背景颜色即可解决,达到我们想要的效果

private void initDialog() {

BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this);
bottomSheetDialog.setContentView(R.layout.dialog_goods_sku_layout);
//给布局设置透明背景色
bottomSheetDialog.getDelegate().findViewById(android.support.design.R.id.design_bottom_sheet)
                .setBackgroundColor(getResources().getColor(android.R.color.transparent));
//  ((View) (dialogView.getParent())).setBackgroundColor(Color.TRANSPARENT)
bottomSheetDialog.show();
 }

需要注意一点,设置背景色setBackgroundColor方法必须要放在setContentView方法之后,否则会报异常的
效果图:
这里写图片描述

BottomSheetDialog 是 Android Support Library 23.2.0 引入的一个新控件,用于实现底部弹出式对话。它继承自 DialogFragment,可以方便地创建底部弹出的对话使用 BottomSheetDialog 的步骤如下: 1. 在 build.gradle 文件中添加依赖: ``` implementation 'com.google.android.material:material:1.2.1' ``` 2. 在布局文件中定义 BottomSheetDialog 的布局: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/bottom_sheet" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <!-- 定义 BottomSheetDialog 的布局 --> </LinearLayout> ``` 3. 在代码中创建 BottomSheetDialog 对象: ``` BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(context); View bottomSheetView = LayoutInflater.from(context).inflate(R.layout.bottom_sheet_layout, null); bottomSheetDialog.setContentView(bottomSheetView); bottomSheetDialog.show(); ``` 在上述代码中,我们先创建了一个 BottomSheetDialog 对象,然后通过 LayoutInflater 加载了布局文件,最后将布局文件设置给 BottomSheetDialog 并显示出来。 BottomSheetDialog 默认会在底部弹出,可以通过设置 setPeekHeight() 方法来设置弹出高度,也可以通过设置 setCancelable() 方法来设置是否可取消。 总之,BottomSheetDialog 是一个非常方便的控件,可以用于实现底部弹出的对话效果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值