android底部弹窗的几种实现方式

本文介绍了Android中创建底部弹窗的四种方法:Dialog、Activity、DialogFragment和BottomSheet。通过Dialog实现简单,而Activity能更好地管理生命周期。DialogFragment提供更好的生命周期管理,官方推荐使用。BottomSheet是自Android 23.2版本起支持的新控件,需要依赖库才能使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述

实现底部弹窗有以下几种方式:
1.dialog
2.Activity
3.DialogFragment
4.bottomSheet

底部弹窗本质上就是将对话框显示在底部。

在开始之前先给出布局文件:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    >

    <LinearLayout
        android:layout_width="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center_vertical"
        android:layout_height="wrap_content">
        <Button
            android:layout_gravity="center_horizontal"
            android:onClick="onDialog"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="dialog"/>
        <Button
            android:layout_gravity="center_horizontal"
            android:onClick="onActivity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="activity"/>
        <Button
            android:layout_gravity="center_horizontal"
            android:onClick="onDialogFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="DialogFragment"/>
        <Button
            android:layout_gravity="center_horizontal"
            android:onClick="onButtonSheet"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="ButtonSheet"/>
    </LinearLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width=
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值