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));
bottomSheetDialog.show();
 }

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

猜你喜欢

转载自blog.csdn.net/hzw2017/article/details/80275098