简单使用BottomSheetBehavior实现底部弹窗

简介: 这次带来的是BottomSheetBehavior的简单使用,BottomSheetBehavior是Android Support Library23.2中引入的,它可以轻松实现底部动作条功能。

这次带来的是BottomSheetBehavior的简单使用,BottomSheetBehavior是Android Support Library23.2中引入的,它可以轻松实现底部动作条功能。

img_6852dae42410c0530bba63e561e5672b.gif

使用方法

●xml布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">
        //按钮要使用布局包裹否则会浮在BottomSheetBehavior之上
        <Button
            android:layout_width="wrap_content"
            android:text="打开"
            android:id="@+id/bt"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/design_bottom_sheet1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" //这个高度决定了BottomSheetBehavior的总高度
        android:background="@color/colorAccent"
        app:behavior_hideable="true" //可以隐藏
        app:behavior_peekHeight="300dp" //设置弹出时的高度
        android:orientation="vertical"
        app:elevation="6dp"
        app:layout_behavior="@string/bottom_sheet_behavior" //这一句固定要加>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:src="@mipmap/ic_launcher"
            android:layout_marginTop="20dp"
            android:layout_height="wrap_content" />
    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

●Activity代码

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final BottomSheetBehavior bottomSheetBehavior=BottomSheetBehavior.from(findViewById(R.id.design_bottom_sheet1));
        //设置默认先隐藏
        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
        findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            //根据状态不同显示隐藏
                if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_HIDDEN) {
                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                } else if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED) {
                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
                }
            }
        });
        //设置监听事件   
        bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                //拖动
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                //状态变化
            }
        });
    }
}

扩展(BottomSheetDialogFragment实现底部弹窗)

img_f81033b34d1545a7097183953d7e98cf.gif

●xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="match_parent"
        android:background="@color/colorAccent"
        android:layout_centerHorizontal="true"
        android:src="@mipmap/ic_launcher"
        android:layout_height="200dp" />
</RelativeLayout>

●BottomSheetDialogFragment代码

public class BottomSheetDialogFragmenttest extends BottomSheetDialogFragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.test,container,false);
    }
}

●Activity代码

findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BottomSheetDialogFragmenttest bottomSheetDialogFragmenttest=new BottomSheetDialogFragmenttest();
               bottomSheetDialogFragmenttest.show(getSupportFragmentManager(),BottomSheetDialogFragmenttest.class.getSimpleName());
            }
        });
目录
相关文章
|
XML 物联网 API
Android Ble蓝牙App(五)数据操作
Android Ble蓝牙App(五)数据操作
2002 0
|
Android开发
Android Studio中修改gradle插件版本和Gradle版本
Android项目中,我们一般要设置gradle插件版本和gradle版本。 项目根目录下的build.gradle文件中,通过classpath可以指定gradle插件的版本。
|
Android开发
Android经典实战之Textview文字设置不同颜色、下划线、加粗、超链接等效果
本文介绍了 `SpannableString` 在 Android 开发中的强大功能,包括如何在单个字符串中应用多种样式,如颜色、字体大小、风格等,并提供了详细代码示例,展示如何设置文本颜色、添加点击事件等,助你实现丰富文本效果。
1168 4
|
12月前
|
人工智能 自然语言处理 算法
接入DeepSeek需要做算法备案吗?一文读懂算法备案的那些事儿
在AI快速发展的今天,算法备案成为企业合规运营的关键。本文通过五个案例解析接入DeepSeek是否需备案:1) 微调模型需备案,流程4-6个月;2) 面向公众服务需备案;3) 内部使用通常无需备案;4) 个人自用无需备案;5) 面向特定专业人士通常无需备案。了解这些要求,确保企业在享受AI红利的同时合规运营,规避风险。
|
XML Java 开发工具
在Android中使用ProgressBar显示进度
在Android中使用ProgressBar显示进度
1816 2
|
XML API 数据格式
Fragment 这些 API 已废弃,你还在使用吗?
Fragment 这些 API 已废弃,你还在使用吗?
412 1
|
XML Java Android开发
Android Studio App开发之实现简单的启动引导页ViewPager(附源码 实现App的欢迎页面)
Android Studio App开发之实现简单的启动引导页ViewPager(附源码 实现App的欢迎页面)
1394 2
|
数据可视化 测试技术 uml
【掌握绘图艺术】用PlantUML绘制完美UML图表,开发者的福音
【掌握绘图艺术】用PlantUML绘制完美UML图表,开发者的福音
3705 2
|
XML Java Android开发
Android Studio App开发之使用摄像机录制视频和从视频库中选取视频的讲解及实战(附源码)
Android Studio App开发之使用摄像机录制视频和从视频库中选取视频的讲解及实战(附源码)
1057 1