AndroidASD完全解析05之AppBarLayout

前面我们介绍过ASD库中的其它几个控件,我们这一篇介绍另外一个控件--AppBarLayout。APPBarLayout继承自LinearLayout,布局方向是垂直方向,我们可以将它当成垂直布局的LinearLayout来使用。不过APPBarLayout在LinearLayout的基础之上加了一些材料设计的概念,它可以让我们定制当某个可以滚动的View的滚动手势发生变化的时候,其内部字View实现的动作。

概述

AppBarLayout是继承于LinearLayout的ViewGroup容器,支持手势滑动,它将包裹的组件都作为AppBar。AppBarLayout里面的View都是通过设置app:layout_scrollFlags属性控制滑动,Google提供了4种滑动表现类型,分别是:

  • scroll:表示向下滚动的时候,设置了这个属性的View会被滚出屏幕范围,直到消失

  • enterAlways:表示向上滚动的时候,设置了这个属性的View会随着滚动手势逐渐出现,直到恢复原来设置的位置

  • enterAlwaysCollapsed:是enterAlways的附加选项,一般跟enterAlways一起使用,它是指,View在往下“出现”的时候,首先是enterAlways效果,当View的高度达到最小高度时,View就暂时不去往下滚动,直到ScrollView滑动到顶部不再滑动时,View再继续往下滑动,直到滑到View的顶部结束。

  • exitUntilCollapsed:值设为exitUntilCollapsed的View,当这个View要往上逐渐“消逝”时,会一直往上滑动,直到剩下的的高度达到它的最小高度后,再响应ScrollView的内部滑动事件。

其中,我们最常用的就是scroll和enterAlways

注意:如果Toolbar和AppBarLayout一起使用的时候,ToolBar需要作为AppBarLayout的子View

google官方推荐的是AppBarLayout需要与一个滚动的视图一起使用,Android给我们提供了一个滚动的视图NestedScrollView,在v4包中,NestedScrollView继承的也是FrameLayout,但是我们可以把它当成ScrollView使用。AppBarLayout要与NestedScrollView关联起来,需要设置一个属性: app:layout_behavior="@string/appbar_scrolling_view_behavior",其中它就是指定Behavior的,appbar_scrolling_view_behavior对应的类的名称是:android.support.design.widget.AppBarLayout$ScrollingViewBehavior,关于Behavior,我们会在后面详细讲解,现在知道需要设置这个就可以了。

使用

上面介绍的都是一些概念性的东西,下面我们通过实例体会一下AppBarLayout的使用方法,这里实现一个简单的例子。

首先是布局文件代码:

<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar

        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        app:layout_collapseMode="pin"
        app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/custom_text"
        android:textSize="18sp"/>

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

这里面的话也没有很多可以讲的了,基本都是在上面讲过了的,下面看一下Activity的代码:

package com.example.adsdemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

/**
 * Created by Devin on 2016/8/16.
 */
public class ABLActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_abl);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("ABLActivity使用");
    toolbar.setNavigationIcon(R.drawable.back);
    setSupportActionBar(toolbar);
}
}

这里也很简单,不过需要注意的是,需要在清单文件中调用没有ActionBar的一个Theme,不然会出错。下面看一下实现的效果图:

2267876-d4827d933efa1a9f.gif

在这里例子中,只用了Android提供的四个Flag中的两个scroll和enterAlways,有兴趣的可以使用其他的两个体会一下。这里就简单介绍了AppBarLayout的使用,等我们学完了其它的组件的时候再一起总结一下,最后附上AppBarLayout的国内镜像API,猛戳这里

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值