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的使用
发布评论