Android Support 23.2 BottomSheetBehavior的使用

编程入门 行业动态 更新时间:2024-10-08 06:17:43

Android <a href=https://www.elefans.com/category/jswz/34/1768979.html style=Support 23.2 BottomSheetBehavior的使用"/>

Android Support 23.2 BottomSheetBehavior的使用

Android Support 23.2前几天新鲜出炉,赶紧磨刀霍霍向牛羊

一:底部菜单犹抱琵琶半遮面(即peekHeight=56dp)

1,新建工程BottomSheetBehavior

compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {applicationId "com.example.bottomsheetbehavior"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}

dependencies {compile fileTree(dir: 'libs', include: ['*.jar'])testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.0'
    compile 'com.android.support:design:23.2.0'
}

2,xml布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android=""
    xmlns:app=""
    xmlns:tools=""
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.bottomsheetbehavior.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/design_bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:behavior_hideable="true"
        app:behavior_peekHeight="56dp"
        app:layout_behavior="@string/bottom_sheet_behavior">

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="56dp"
                android:gravity="center"
                android:text="BottomSheets"/>

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="centerCrop"
                android:src="@mipmap/bg"/>

        </LinearLayout>


    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email"/>

</android.support.design.widget.CoordinatorLayout>

好了,现在可以运行看下:(小米2原生android6.0.1)

可以看到,TextView向下隐藏了一点(当时还在找BottomSheetBehavior的资料时看到了下面这个)

在父控件中添加了一句android:fitsSystemWindows="true",是为了改变statusBar的颜色,Material Design嘛!
这个button就是少了statusBar的高度了,所以就改了下代码,思路是,当创建bottomsheets时向上移动statusbar的高度,当 bottomsheets发生移动时,就让bottomsheets的高度还原,思路是这样了,贴代码,省的以后用到了忘记( shu/p/7b1bb6c0c1f0#)

3,所以,代码修改如下:

View bottomSheet = findViewById(R.id.design_bottom_sheet);
bottomSheet.setTranslationY(-getStatusBarHeight(this));
BottomSheetBehavior sheetBehavior = BottomSheetBehavior.from(bottomSheet);
sheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {boolean first = true;

    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {}@Override
    public void onSlide(@NonNull View bottomSheet, float slideOffset) {if (first) {first = false;
            bottomSheet.setTranslationY(0);
        }}
});
运行结果如下:




二:底部菜单完全隐藏(即peekHeight=0dp or 不设置peekHeight) 

在第一部分的第二步设置peekHeight=0或者不设置peekHeoght,再运行一遍,坑爹的恐怖分子,android5.x平台滑动不出来!!手动设置也没有!! 然后再 但是我的做法是,在第一部分里只更改xml里的peekHeight(peekHeight=0或者不设置peekHeoght),一样解决了这个问题~~

大神源码解析:.md 解坑:
 
 
          

更多推荐

Android Support 23.2 BottomSheetBehavior的使用

本文发布于:2024-02-06 09:10:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1747916.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Support   Android   BottomSheetBehavior

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!