Android Support Design库之AppBarLayout
来源:互联网 发布:验证身份证18位数的js 编辑:程序博客网 时间:2024/05/20 04:47
AppBarLayout跟它的名字一样,把容器内的组件全部作为AppBar,就像下面将一个ToolBar和TabLayout包裹在一起。
1.在build.gradle
中添加依赖库的支持
compile 'com.android.support:design:23.2.0'compile 'com.android.support:cardview-v7:23.4.0'
2.在布局中使用AppBarLayout并将Toolbar包裹
<?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="com.zsy.appbarlayout.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar android:id="@+id/bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/bar_color" android:minHeight="?attr/actionBarSize" app:layout_scrollFlags="scroll|enterAlways" app:popupTheme="@style/ToolbarPopupTheme" app:theme="@style/ToolBarTheme" /> <android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/bar_color" app:tabIndicatorColor="#fff" app:tabSelectedTextColor="#fff" app:tabTextAppearance="?android:attr/textAppearanceMedium" app:tabTextColor="#d7d4d4" /> </android.support.design.widget.AppBarLayout> <android.support.v7.widget.RecyclerView android:id="@+id/recy" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> <android.support.design.widget.TabLayout android:id="@+id/tab_layout_two" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="#fff" app:layout_behavior="com.zsy.appbarlayout.BottomBehavior" app:tabIndicatorColor="#00000000" app:tabSelectedTextColor="#3b5abe" app:tabTextColor="#484747" /></android.support.design.widget.CoordinatorLayout>
3.使用CoordinatorLayout与自定义Behavior实现将底部导航栏滚出屏幕
public class BottomBehavior extends CoordinatorLayout.Behavior<View> { public BottomBehavior(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) { //这个方法是说明这个子控件是依赖AppBarLayout的 return dependency instanceof AppBarLayout; } @Override public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) { float translationY = Math.abs(dependency.getTop());//获取跟随布局的顶部位置 child.setTranslationY(translationY); return true; }}
4.来看下实现的效果
5.一个可以滚动的组件,例如RecyclerView、ListView(这里需要注意的是,貌似只支持RecyclerView、ListView,如果你用一个ScrollView,是没有效果的)。如果:
1、给这个可滚动组件设置了layout_behavior
2、给另一个控件设置了layout_scrollFlags那么,当设置了layout_behavior的控件滑动时,就会触发设置了layout_scrollFlags的控件发生状态的改变。
6.layout_scrollFlags具有以下几个属性
- scroll: 所有想滚动出屏幕的view都需要设置这个flag没有设置这个flag的view将被固定在屏幕顶部。
- enterAlways: 这个flag让任意向下的滚动都会导致该view变为可见,启用快速“返回模式”。
enterAlwaysCollapsed: 当你的视图已经设置minHeight属性又使用此标志时,你的视图只能已最小高度进入,
只有当滚动视图到达顶部时才扩大到完整高度。需要注意的是,后面两种模式基本只有在CollapsingToolbarLayout才有用,而前面两种模式基本是需要一起使用的,
也就是说,这些flag的使用场景,基本已经固定了
7.滑动列表的时候将ToolBar和底部滚出屏幕,来增大用户交互内容大小使的操作更加方便。如果对RecyclerView和TabLayout还不太熟悉的可以阅读这两篇RecyclerView,TabLayout。
项目地址:https://github.com/azhong1011/AppBarLayout
2 0
- Android Support Design库之AppBarLayout
- Android Support Design库之AppBarLayout
- Android Design Support Library之CoordinatorLayout,AppBarLayout
- Android Design Support Library(二):CoordinatorLayout、AppBarLayout简单用法
- Android Material Design学习之二AppBarLayout
- Android:Material Design之CoordinatorLayout+AppBarLayout使用
- Android Material Design 之 CoordinatorLayout + AppBarLayout
- Android Material Design学习之二AppBarLayout
- Android Support Design库之TextInputLayout
- Android Support Design库之TabLayout
- Android Support Design库之FloatingActionButton
- android support design Library库之snakeBar
- Android Support Design之Snackbar
- Android Support Design 之 CoordinatorLayout
- android.support.design.widget包下的CoordinatorLayout、AppBarLayout、TextInputLayout、FloatingActionButton、
- Android Material Design之CoordinatorLayout+AppBarLayout实现上滑隐藏
- Android----Material Design之(FloatActionButton,CoordinatorLayout,AppBarLayout等)
- Material Design学习之AppbarLayout
- 将构件发布到maven
- 调整数组顺序使奇数位于偶数前面
- 一个有广告的纸杯子的测试用例设计(黑盒测试用例设计)
- C#控制台基础 C#中引用类型有哪些
- 中介者模式
- Android Support Design库之AppBarLayout
- Intent和PendingIntent
- hdu 1402 A * B Problem Plus[【FFT】
- MySQL基本概念
- linux常用命令和文件操作命令
- 单片机小项目总结
- 51Nod 1393 0和1相等串
- 方法重写与方法重载 静态
- Mysql 存储引擎中InnoDB与Myisam的主要区别