Android---底部弹窗之BottomSheetDialog

这篇具有很好参考价值的文章主要介绍了Android---底部弹窗之BottomSheetDialog。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

android bottomsheetdialog,# Material Design,android

BottomSheetDialog 是Android开发中的一个弹出式对话框,它从屏幕底部弹出并覆盖部分主界面。

1. BottomSheetDialog的使用

// 参数2:设置BottomSheetDialog的主题样式;将背景设置为transparent,这样我们写的shape_bottom_sheet_dialog.xml才会起作用
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this, R.style.BottomSheetDialog);
//不传第二个参数
//BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this);

// 底部弹出的布局
View bottomView = LayoutInflater.from(requireContext()).inflate(R.layout.bottom_sheet_layout, null);

bottomSheetDialog.setContentView(bottomView);
//设置点击dialog外部不消失
//bottomSheetDialog.setCanceledOnTouchOutside(false);
bottomSheetDialog.show();

2.加载布局

bottom_sheet_layout.xml ; 通过LayoutInflater拿到底部弹窗布局后,通过setContentView()把布局加载到BottomSheetDialog中。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/shape_bottom_sheet_dialog">

    <TextView
        android:id="@+id/choose_photo"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="从手机相册选择"
        android:textSize="15sp"
        android:textColor="#191919"
        android:gravity="center"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#F5F5F5"/>
    <TextView
        android:id="@+id/check_photo"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="查看上一张头像"
        android:textSize="15sp"
        android:textColor="#191919"
        android:gravity="center"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#F5F5F5"/>
    <TextView
        android:id="@+id/save_photo"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="保存到手机"
        android:textSize="15sp"
        android:textColor="#191919"
        android:gravity="center"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:background="#F5F5F5"/>
    <TextView
        android:id="@+id/cancel"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="取消"
        android:textSize="15sp"
        android:textColor="#191919"
        android:gravity="center"/>

</LinearLayout>

3.显示

通过调用  BottomSheetDialg.show()方法就能将底部弹窗给显示出来。

4. BottomSheetDialog 圆角设置

android bottomsheetdialog,# Material Design,android

写一个shape,在Drawable下创建一shape_bottom_sheet_dialog.xml,里面设置圆角的样式。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners android:topLeftRadius="@dimen/dime_10dp"
        android:topRightRadius="@dimen/dime_10dp"/>
    <solid android:color="@color/white"/>

</shape>

把bottom_sheet_layout.xml的整个背景设置为shape_bottom_sheet_dialog.xml这个shape。

android bottomsheetdialog,# Material Design,android

通过上面的设置,圆角效果并没有展示出来。 还需要将BottomSheetDialog的背景设置为透明。在themes.xml(res->values->themes)添加下面两个style。

    <!--实现BottomSheetDialog圆角效果-->
    <style name="BottomSheetDialog" parent="Theme.Design.Light.BottomSheetDialog">
        <item name="bottomSheetStyle">@style/bottomSheetStyleWrapper</item>
    </style>
    <style name="bottomSheetStyleWrapper" parent="Widget.Design.BottomSheet.Modal">
        <item name="android:background">@android:color/transparent</item>
    </style>

在new BottomSheetDialog()的第二个参数,传入这个style.文章来源地址https://www.toymoban.com/news/detail-758979.html

new BottomSheetDialog(this, R.style.BottomSheetDialog);

到了这里,关于Android---底部弹窗之BottomSheetDialog的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【微信小程序】底部弹窗固定定位fixed+textarea,导致的adjust-position在安卓手机失效问题

    安卓手机测试时候发现,fixed固定定位导致键盘弹出时,textarea无法正常被顶上去。 然后我就尝试了第二种方法用scroll-view将高度设置为100vh时,将页面保持在视口高度,这个时候发现不管是弹窗是fixed还是absolute都无法让安卓手机的键盘弹出时textarea顶上去 最后我用了最后一种

    2024年01月18日
    浏览(38)
  • Android手机底部安全区域问题

    这个问题主要体在全面屏手机上,统称为安全区域 直接上图(不知道怎么调正,😓) 这个问题刚开始困扰了我好久, 但是去看抖音发现它底部也是有安全区域的,才知道在Android上是无法完全消除的 (第二种的方法是可以消除掉的,但是会影响用户体验) 而抖音也该也是采用

    2024年02月09日
    浏览(27)
  • android中实现底部导航栏

            底部导航栏在app应用中是十分常见了,大部分的安卓应用中也都实现了底部导航栏的功能,这里我就以我以前做的一个简单小说阅读软件为例,为大家演示一下底部导航栏的使用,需要的朋友直接复制代码过去改写就行了。         这里包含了一些进行操作实际

    2024年02月19日
    浏览(32)
  • uniapp android底部弹框

    uniapp android底部弹框,带有动画效果

    2024年02月14日
    浏览(28)
  • Android BottomNavigation底部导航栏使用

    原文地址: Android BottomNavigation底部导航栏使用 - Stars-One的杂货小窝 本文侧重点记录一些特殊的样式设置,所以基本使用这里就简单概述一下,详细图文可以去找其他人的博文 1.创建对应的menu菜单文件 2.xml布局引用menu菜单 3.启动Activity预览效果 可以使用 setOnItemSelectedListener 方法监

    2024年02月12日
    浏览(35)
  • Android---DslTabLayout实现底部导航栏

    1. 在 Android 项目中引用 JitPack 库  AGP 8.+ 根目录的 settings.gradle  AGP 8.+ 根目录如果是  settings.gradle.kts 文件  AGP 8.- 根目录的 build.gradle 2. 添加依赖  在APP目录中的 build.gradle 3. 布局 activity_main.xml 4. 底部 tab_item.xml 布局 5. 每个底部 tab 对应的 fragment a. DiscoveryFragment b. fragment_dis

    2024年02月20日
    浏览(29)
  • Android底部导航栏之BottomNavigationView

    1,首先需要添加依赖: implementation \\\'com.google.android.material:material:1.1.0\\\' 2,布局文件中引入: 3,常用属性: app:itemTextColor 文字的颜色,可以通过 selector 来控制选中和未选中的颜色 app:itemIconTint 图标的颜色,可以通过 selector 来控制选中和未选中的颜色 app:itemIconSize 图标大小,

    2024年02月08日
    浏览(60)
  • android——spinner下拉弹窗、popupwindow下拉弹窗列表

    效果图如下: adapter的代码: xml代码: MainActivity代码 具体的代码下载地址为:https://download.csdn.net/download/wy313622821/88274359 效果图为:     代码下载地址为: https://download.csdn.net/download/wy313622821/88275750

    2024年02月09日
    浏览(38)
  • Android底部小白条的解决方法

    大家好,我国著名数学家华罗庚先生曾说:聪明在于勤奋,天才在于积累。         在Android开发中,我们常常需要给App界面设置全屏沉浸式。但是在下发现了一个问题,当系统的手势提示条出现的时候,界面的底部就出现了一个小白条,非常影响UI的美观。如下图所示:  

    2024年02月11日
    浏览(25)
  • Android14弹窗问题

    升级安卓14后,应用没有适配对应的CPU架构而被系统强制弹窗,弹窗流程可见参考应用弹窗“此应用专为旧版Android打造,因此可能无法正常运行…”的原因,应用层面没办法去干扰这个流程,应该是在安装时有检测相关的包。 弹窗共有两个:此应用与最新版 Android 不兼容。请

    2024年02月04日
    浏览(23)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包