前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >Android BottomSheetDialog使用实现底部拖动弹窗

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

作者头像
晨曦_LLW
发布2020-09-25 13:27:45
发布2020-09-25 13:27:45
2.4K00
代码可运行
举报
运行总次数:0
代码可运行

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

然后进入实际操作环节。

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

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

代码语言:javascript
代码运行次数:0
复制
implementation 'com.google.android.material:material:1.0.0'

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

然后创建一个弹窗的dialog_bottom_new.xml布局。 代码如下:

代码语言:javascript
代码运行次数:0
复制
<?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是这个

代码语言:javascript
代码运行次数:0
复制
<?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>

里面的图片是这个

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

代码语言:javascript
代码运行次数:0
复制
<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中写入一个方法:

代码语言:javascript
代码运行次数:0
复制
	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();
    }

运行一下:

拜拜~

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/08/17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档