@null
当前位置:   article > 正文

Android BottomSheetDialogFragment底部弹出栏的几种用法(圆角、禁止滑动收起)_bottomsheetdialogfragment 底部弹窗需要两个圆角

bottomsheetdialogfragment 底部弹窗需要两个圆角

1.引入

implementation 'com.android.support:design:29.1.0'
  • 1

在这里插入图片描述

2.简单用法

在这里插入图片描述

  • BottomDialogFragment1其中R.layout.fragment_bottom_dialog 是dialog的布局,父布局的layout_height需要设置成wrap_content。里面的子布局可以设置高度。
package com.zhangyu.bottomsheetdialog;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

public class BottomDialogFragment1 extends BottomSheetDialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_bottom_dialog, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {

    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 调用的地方
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_test_1:
                new BottomDialogFragment1().show(getSupportFragmentManager(),"BottomDialogFragment1");
                break;
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

3.设置圆角

在这里插入图片描述

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

    <solid android:color="#FF402416" />

    <corners
        android:topLeftRadius="16dp"
        android:topRightRadius="16dp" />
</shape>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 设置默认背景透明(解决设置圆角时默认背景白色问题)
    <style name="BottomDialog" parent="@style/Base.V7.Theme.AppCompat.Light.Dialog">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <!--动画设置-->
        <item name="android:windowAnimationStyle">@style/Animation.Design.BottomSheetDialog</item>
    </style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • BottomDialogFragment2中,在onCreateDialog中设置setStyle(STYLE_NO_TITLE, R.style.BottomDialog);
public class BottomDialogFragment2 extends BottomSheetDialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        setStyle(STYLE_NO_TITLE, R.style.BottomDialog);
        return super.onCreateDialog(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_bottom_dialog, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {

    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

4.禁止滑动收起底部栏

  • onCreateView中增加了一个 BottomSheetBehavior behavior=BottomSheetBehavior.from(getDialog().findViewById(R.id.design_bottom_sheet));
    通过behavior来控制隐藏行为
  • 需要在post中进行获取getDialog否则获取不到null。java.lang.NullPointerException: Attempt to invoke virtual method ‘android.view.View android.app.Dialog.findViewById(int)’ on a null object reference
public class BottomDialogFragment3 extends BottomSheetDialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        setStyle(STYLE_NO_TITLE, R.style.BottomDialog);
        return super.onCreateDialog(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_bottom_dialog, container, false);
        view.post(new Runnable() {
            @Override
            public void run() {
                //R.id.design_bottom_sheet基本是固定的,不用担心后面API的更改
                BottomSheetBehavior behavior=BottomSheetBehavior.from(getDialog().findViewById(R.id.design_bottom_sheet));
                behavior.setHideable(false);//此处设置表示禁止BottomSheetBehavior的执行
            }
        });
        initView(view);
        return view;
    }

    private void initView(View view) {

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

5.传递参数重写show方法


    public void show(@NonNull FragmentManager manager, @Nullable String tag, String userName, String industry) {
        super.show(manager, tag);
        this.userName = userName;
        this.industry = industry;
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

设置弹窗的弹出高度

        binding.root.post {
            //如果是小屏手机
            if (ScreenUtils.getAppScreenHeight() <= 1920) {
                val behavior = BottomSheetBehavior.from(requireDialog().findViewById(R.id.design_bottom_sheet))
                behavior.peekHeight = binding.airPanelMainLayout.measuredHeight
            }
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

侧边弹出栏

https://blog.csdn.net/yu540135101/article/details/108756593

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/770022
推荐阅读
相关标签