说说 Android 的 Material Design 设计(五)——可折叠式标题栏

1 CollapsingToolbarLayout 布局

CollapsingToolbarLayout 是基于 Toolbar 的布局。它可以让 Toolbar 的效果变得更加华丽。

注意:CollapsingToolbarLayout 只能作为 AppBarLayout 的直接子布局。

现在我们创建一个空活动来显示猫的详情:

然后在 activity_cat.xml 中编写界面布局,主要分为两个部分,一个是标题栏,另一个是内容:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:material="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--标题栏布局-->
    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="250dp">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            material:contentScrim="?attr/colorPrimary"
            material:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/cat_image_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                material:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                material:layout_collapseMode="pin" />

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

    <!--内容布局-->
    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        material:layout_behavior="@string/appbar_scrolling_view_behavior">

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

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="15dp"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_marginTop="35dp"
                material:cardCornerRadius="4dp">

                <TextView
                    android:id="@+id/cat_text_view"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="10dp" />
            </android.support.v7.widget.CardView>

        </LinearLayout>

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

    <!--用于评论的悬浮按钮-->
    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:src="@drawable/comment"
        material:layout_anchor="@id/app_bar_layout"
        material:layout_anchorGravity="bottom|end" />
</android.support.design.widget.CoordinatorLayout>
  1. 整体布局使用 CoordinatorLayout。
  2. 定义了 xmlns:material="http://schemas.android.com/apk/res-auto" 命名空间。

标题栏布局:

  1. 标题栏布局使用 AppBarLayout,内层嵌套了 CollapsingToolbarLayout。
  2. CollapsingToolbarLayout 中的 contentScrim 可指定当标题栏趋于折叠以及折叠之后的背景色;而 layout_scrollFlags 中的 scroll 表示 CollapsingToolbarLayout 会随着内容的滚动而滚动; layout_scrollFlags 中的 exitUntilCollapsed 表示折叠后会保留在界面中(不会被移出屏幕)。
  3. 在 CollapsingToolbarLayout 中,定义了 ImageView 与 Toolbar。即这个标题栏是由普通标题栏加图片组合而成的。其中有一个 layout_collapseMode 属性,它用于设置折叠模式。pin 表示在折叠过程中使用保持,就像被大头针定住了一样;parallax 表示在折叠过程中会产生视差。

内容布局:

  1. 外层使用 NestedScrollView,它不仅支持使用滚动的方式来查看屏幕外的数据,还能嵌套响应滚动事件。我们还通过 layout_behavior 来指定布局行为。
  2. 因为 NestedScrollView 内部只允许存在一个直接的子布局,所以我们可以先嵌套一个 LinearLayout,然后再其内部放入更多内容。
  3. 这里使用 TextView 来显示内容,然后把它放入 CardView (卡片式布局)中。
  4. 我们为 CardView 设置了外边距,还设置了圆角。这些工作都是为了让页面变得更好看。

最后我们还加了一个悬浮按钮(FloatingActionButton):

  1. 首先把按钮图片 png ,放在 drawable 文件夹下。
  2. 通过 layout_anchor 来设置锚点,让悬浮按钮出现在标题栏区域内。
  3. 通过 layout_anchorGravity 让按钮出现在标题栏区域的右下角。

2 活动类

public class CatActivity extends AppCompatActivity {


    /**
     * 猫名
     */
    public static final String CAT_NAME = "cat_name";

    /**
     * 猫图片 ID
     */
    public static final String CAT_IMAGE_ID = "cat_image_id";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cat);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {//启用 HomeAsUp 按钮(返回箭头)
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        Intent intent = getIntent();

        //设置标题
        CollapsingToolbarLayout layout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout);
        String catName = intent.getStringExtra(CAT_NAME);
        layout.setTitle(catName);

        //设置图片
        int catImageId = intent.getIntExtra(CAT_IMAGE_ID, 0);
        ImageView imageView = (ImageView) findViewById(R.id.cat_image_view);
        Glide.with(this).load(catImageId).into(imageView);//加载图片

        //设置内容
        TextView contentView = (TextView) findViewById(R.id.cat_text_view);
        contentView.setText(generateContent(catName));

    }

    /**
     * 生成内容
     *
     * @param catName 猫名
     * @return
     */
    private String generateContent(String catName) {
        //实践中,会根据参数名称,返回对应的内容
        return "俄罗斯蓝猫(Russian Blue),历史上曾被称做阿契安吉蓝猫,曾有过三种不同的名字。最初是阿契安吉蓝猫,直到20世纪40年代才有现在的名字,另外有段时间它则叫做马耳他猫。证据显示,这种猫确实原产于俄罗斯,因为已在俄罗斯寒带地区发现了同种的猫。俄罗斯蓝猫体型细长,大而直立的尖耳朵,脚掌小而圆,走路像是用脚尖在走。身上披着银蓝色光泽的短被毛,配上修长苗条的体型和轻盈的步态,尽显一派猫中的贵族风度。";
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home://点击 HomeAsUp 按钮时
                finish();//关闭当前活动(即返回上一个活动)
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
  1. 我们在标题栏启用 HomeAsUp 按钮(返回箭头),用于关闭当前活动。
  2. 将 CollapsingToolbarLayout 传递过来的猫类型,作为标题。
  3. 接着使用 Glide 来加载图片。
  4. 然后设置内容。
  5. 最后在 onOptionsItemSelected 中处理 HomeAsUp 按钮的点击事件。

现在我们要处理首页活动 RecyclerView 的点击事件,当点击某只猫时,打开它的详情活动页:

public class CatAdapter extends RecyclerView.Adapter<CatAdapter.ViewHolder> {

    private Context context;

    private List<Cat> cats = new ArrayList<>();

    public CatAdapter(List<Cat> cats) {
        this.cats = cats;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {//设置上下文环境
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.cat_item, parent, false);

        //处理 RecyclerView 点击事件
        final ViewHolder holder = new ViewHolder(view);
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Cat cat = cats.get(position);
                Intent intent = new Intent(context, CatActivity.class);
                intent.putExtra(CatActivity.CAT_NAME, cat.getType());
                intent.putExtra(CatActivity.CAT_IMAGE_ID, cat.getImgId());
                context.startActivity(intent);
            }
        });

        return holder;
    }

   ...
}

运行效果:

3 融合系统顶部状态栏

利用 fitsSystemWindows 属性我们可以将标题栏中的图片与系统顶部状态栏融合起来。

注意: 因为我们这里的 ImageView 嵌套的很深,所以必须把它以及它的祖先布局中的 fitsSystemWindows 都设置为 true,才能真正生效:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:material="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:fitsSystemWindows="true"
   >

   <!--标题栏布局-->
   <android.support.design.widget.AppBarLayout
       android:id="@+id/app_bar_layout"
       android:layout_width="match_parent"
       android:layout_height="250dp"
       android:fitsSystemWindows="true"
       >

       <android.support.design.widget.CollapsingToolbarLayout
           android:id="@+id/collapsing_toolbar_layout"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
           material:contentScrim="?attr/colorPrimary"
           material:layout_scrollFlags="scroll|exitUntilCollapsed"
           android:fitsSystemWindows="true"
           >

           <ImageView
               android:id="@+id/cat_image_view"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:scaleType="centerCrop"
               material:layout_collapseMode="parallax"
               android:fitsSystemWindows="true"
               />

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

  

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

接着把状态栏的颜色设置为透明,即

<item name="android:statusBarColor">@android:color/transparent</item>

因为这个属性从 API21 (Android 5.0 +)才出现,所以我们必须做差异化实现。

在 res 目录下新建一个 values-v21 目录,然后再新建一个 styles.xml:

<resources>
    <!--Android 5.0+-->
    <style name="catActivityTheme" parent="AppTheme">
        <!--状态栏颜色指定为透明-->
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>
</resources>

这样设计是因为只有 Android 5.0+ 才会去读取该文件。

我们还需要在 values/styles.xml 中新建一个不包含实际内容的 catActivityTheme,确保 Android 5.0 之前的版本不会抛错:

<!--小于 Android 5.0-->
<style name="catActivityTheme" parent="AppTheme">
</style>

最后在 AndroidManifest.xml 中的 CatActivity 活动,设置刚刚定义的主题:

 <activity android:name=".CatActivity"
            android:theme="@style/catActivityTheme"
            ></activity>

运行效果:


就算是现在的刘海屏,也可以适配的很好哦O(∩_∩)O~

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268

推荐阅读更多精彩内容