底部弹出框的实现方式

1. 自定义dialog实现

/**
     * 显示支付窗口
     */
    private void showDialog() {
        dialog = new Dialog(this, R.style.ActionSheetDialogStyle);
        //填充对话框的布局
        inflate = LayoutInflater.from(this).inflate(R.layout.item_dialog_pay, null);
        // 初始化控件
        TextView close = (TextView) inflate.findViewById(R.id.tv_supportpay_close);
        LinearLayout weixin = (LinearLayout) inflate.findViewById(R.id.ll_supportpay_weixin);
        LinearLayout zhifubao = (LinearLayout) inflate.findViewById(R.id.ll_supportpay_zhifubao);
        close.setOnClickListener(this);
        weixin.setOnClickListener(this);
        zhifubao.setOnClickListener(this);
        // 将布局设置给Dialog
        dialog.setContentView(inflate);
        // 获取当前Activity所在的窗体
        Window dialogWindow = dialog.getWindow();
        //最重要的一句话,一定要加上!要不然怎么设置都不行
        dialogWindow.setBackgroundDrawableResource(android.R.color.transparent);
        // 设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        // 获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        Display d = dialogWindow.getWindowManager().getDefaultDisplay();
        //获取屏幕宽
        lp.width = (int) (d.getWidth());
        //宽度按屏幕大小的百分比设置,这里我设置的是全屏显示
        lp.gravity = Gravity.BOTTOM;
        if (lp.gravity == Gravity.BOTTOM)
            lp.y = 20;
        //如果是底部显示,则距离底部的距离是0
        dialogWindow.setAttributes(lp);
        dialog.show();//显示对话框
    }

2. 第三方控件实现bottomsheet

GitHub地址:https://github.com/Flipboard/bottomsheet

使用方法

  • [ 1 ] 还是先依赖
repositories {
    jcenter()
}

dependencies {
    compile 'com.flipboard:bottomsheet-core:1.5.3'
    compile 'com.flipboard:bottomsheet-commons:1.5.3' // optional
}
  • [ 2 ] 在xml文件中使用
<?xml version="1.0" encoding="utf-8"?>
<com.flipboard.bottomsheet.BottomSheetLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/bottom_sheet_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:onClick="show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="三" />
    </LinearLayout>
</com.flipboard.bottomsheet.BottomSheetLayout>



布局弹出内容

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFF000"
    android:orientation="vertical"
    tools:context="com.itheima.app_bottom.MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="条目一" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="条目二" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="条目三" />
</LinearLayout>

  • [ 3 ] 编写显示与隐藏逻辑

public void show(View view) {
        //判断弹出内容是否可见
        if (bottomSheetLayout.isSheetShowing()) {
            //可见则隐藏
            bottomSheetLayout.dismissSheet();
        } else {
            if (popView == null) {
                popView = LayoutInflater.from(this).inflate(R.layout.pop_sheet, bottomSheetLayout, false);
            }
            //不可见则显示 ,对popView进行是否为空判断可以减少view的创建
            bottomSheetLayout.showWithSheetView(popView);
        }
    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,556评论 25 708
  • 太长了,还是转载吧...今天在看博客的时候,无意中发现了@Trinea在GitHub上的一个项目Android开源...
    庞哈哈哈12138阅读 20,282评论 3 283
  • 我想我已经从茫茫大道中我终于找到了自己的一条路:精益生产。此篇文章为我的第一篇读精益生产方面的读书笔记,主要记录我...
    我在时光深处等你阅读 1,676评论 1 1
  • 约束是一种限制,它通过对表的行或列的数据做出限制,来确保表的数据的完整性、唯一性。 约束分类 |约束类型 |主...
    lupinwu阅读 183评论 0 0
  • 姓名:魏正君《六项精进》第270期感谢2组 公司:绵阳大北农农牧科技有限公司 【日精进打卡第22天】 【知~学习】...
    莫心莫肺阅读 145评论 0 0