Android BottomSheetDialog使用实现底部拖动弹窗

简介: Android BottomSheetDialog使用实现底部拖动弹窗

为了不浪费你的时间,先看一下效果图。


20200817155808850.gif


然后进入实际操作环节。


 平时我们使用其他APP时对于评论这快,通常都是点击之后底部弹窗一个窗口,高度是各不相同,而且如果没有占满屏幕的话还可以往上拖,直到吸附在顶部,感觉是挺有意思的,但其实做起来没有那么难,这篇文章就是以一个新手刚接触这个功能的视觉来写的,好了,新建一个项目吧。


2020081709261380.png



BottomDialogDemo建好之后先在app下的build.gradle中添加一个依赖


implementation 'com.google.android.material:material:1.0.0'


添加位置如下图所示,添加只有记得右上角Sync一下,否则不生效的。


20200817160010103.png


然后创建一个弹窗的dialog_bottom_new.xml布局。


代码如下:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/shape_dialog_bg">
    <TextView
        android:text="弹窗标题"
        android:gravity="center"
        android:textColor="#000"
        android:textSize="16sp"
        android:padding="16dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <View
        android:layout_width="match_parent"
        android:background="#000"
        android:layout_height="0.4dp"/>
    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <ImageView
                android:src="@drawable/code"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
            <ImageView
                android:src="@drawable/code"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>
</LinearLayout>


里面的圆角背景shape_dialog_bg.xml是这个

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


里面的图片是这个

20200817154535216.png


然后修改activity_main.xml布局代码,增加一个Button


<Button
        android:id="@+id/botton_new_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="New Bottom" />


然后在MainActivity中写入一个方法:


  public void doClick(View view) {
        BottomSheetDialog mBottomSheetDialog = new BottomSheetDialog(this);
        View view1 = getLayoutInflater().inflate(R.layout.dialog_bottom_new, null);
        mBottomSheetDialog.setContentView(view1);
        mBottomSheetDialog.getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundColor(Color.TRANSPARENT);
        mBottomSheetDialog.show();
    }


运行一下:


20200817155808850.gif


拜拜~

相关文章
|
Android开发
flutter中实现仿Android端的onResume和onPause方法
flutter中实现仿Android端的onResume和onPause方法
|
4月前
|
Android开发
UniApp Android 页面拖动,去掉半圆形阴影
UniApp Android 页面拖动,去掉半圆形阴影
45 0
|
4月前
|
XML Java Android开发
Android Studio App开发之实现底部标签栏BottomNavigationView和自定义标签按钮实战(附源码 超详细必看)
Android Studio App开发之实现底部标签栏BottomNavigationView和自定义标签按钮实战(附源码 超详细必看)
77 0
|
4月前
|
XML API Android开发
Android 自定义View 之 Dialog弹窗
Android 自定义View 之 Dialog弹窗
|
4月前
|
XML Java Android开发
Android App开发音量调节中实现拖动条和滑动条和音频管理器AudioManager讲解及实战(超详细 附源码和演示视频)
Android App开发音量调节中实现拖动条和滑动条和音频管理器AudioManager讲解及实战(超详细 附源码和演示视频)
76 0
|
7月前
|
Android开发
Android 弹窗优先级管理,赶紧拿去用吧
Android 弹窗优先级管理,赶紧拿去用吧
|
8月前
|
Android开发
Android 中SeekBar拖动条控件的基本用法
Android 中SeekBar拖动条控件的基本用法
103 0
Android 中SeekBar拖动条控件的基本用法
|
8月前
|
Android开发
Android 自定义弹窗 附带搜索过滤功能
前两天要求在项目中添加个小功能,今天正好有时间随手写了一个小demo,过程分享给大家。以后如果有此类需求可直接移植使用。
|
8月前
|
Android开发
Android底部弹窗的正确打开方式2
Android底部弹窗的正确打开方式
|
8月前
|
XML Android开发 数据格式
Android底部弹窗的正确打开方式1
Android底部弹窗的正确打开方式