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


拜拜~

相关文章
|
9月前
|
XML Java API
20. 【Android教程】拖动条 SeekBar
20. 【Android教程】拖动条 SeekBar
135 3
|
8月前
|
XML Android开发 数据格式
Android 中如何设置activity的启动动画,让它像dialog一样从底部往上出来
在 Android 中实现 Activity 的对话框式过渡动画:从底部滑入与从顶部滑出。需定义两个 XML 动画文件 `activity_slide_in.xml` 和 `activity_slide_out.xml`,分别控制 Activity 的进入与退出动画。使用 `overridePendingTransition` 方法在启动 (`startActivity`) 或结束 (`finish`) Activity 时应用这些动画。为了使前 Activity 保持静止,可定义 `no_animation.xml` 并在启动新 Activity 时仅设置新 Activity 的进入动画。
249 12
|
8月前
|
Android开发 UED
Android采用Scroller实现底部二楼效果
Android采用Scroller实现底部二楼效果
77 0
Android采用Scroller实现底部二楼效果
|
9月前
|
Android开发
Android中如何快速的实现RecycleView的拖动重排序功能
使用`ItemTouchHelper`和自定义`Callback`,在`RecyclerView`中实现拖动排序功能。定义`ItemTouchHelperAdapter`接口,`Adapter`实现它以处理`onItemMove`方法。`SimpleItemTouchHelperCallback`设置拖动标志,如`LEFT`或`RIGHT`(水平拖动),并绑定到`RecyclerView`以启用拖动。完成这些步骤后,即可实现拖放排序。关注公众号“AntDream”获取更多内容。
149 3
|
9月前
|
Android开发 UED
|
9月前
|
XML 前端开发 API
Android中实现Bitmap在自定义View中的放大与拖动
Android中实现Bitmap在自定义View中的放大与拖动
178 1
|
10月前
|
XML Java Android开发
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
894 1
|
10月前
|
Android开发
Android SystemUI去掉拖动亮度条QSPanel界面隐藏功能
Android SystemUI去掉拖动亮度条QSPanel界面隐藏功能
193 0
|
10月前
|
Android开发
Android控件——Checkbox复选框、RadioButton单选、ToggleButton开关、SeekBar拖动条
Android控件——Checkbox复选框、RadioButton单选、ToggleButton开关、SeekBar拖动条
|
10月前
|
XML API Android开发
Android 自定义View 之 Dialog弹窗
Android 自定义View 之 Dialog弹窗
306 1

热门文章

最新文章