在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

通过来模仿稀土掘金个人页面的布局来学习使用CoordinatorLayout

2016-8-16 12:47| 发布者: zhangjf| 查看: 609| 评论: 0

摘要: 特别喜欢稀土掘金个人界面的样子,那我们就来看看如何实现这个效果吧,要想实现这个效果,肯定需要的是Material Design风格,那就需要学会使用以下控件:CoordinatorLayout,AppBarLayout,CollapsingToolbarLayout ...

特别喜欢稀土掘金个人界面的样子,那我们就来看看如何实现这个效果吧,要想实现这个效果,肯定需要的是Material Design风格,那就需要学会使用以下控件:CoordinatorLayout,AppBarLayout,CollapsingToolbarLayout,Toolbar,TabLayout等,如果你做出这个效果来,那这些控件你就基本掌握了。

效果对比图

介绍之前,我们先来看看效果对比图:

稀土掘金app原图

模仿的效果图

CoordinatorLayout的介绍

CoordinatorLayout作为“super-powered FrameLayout”基本实现两个功能:

作为顶层布局

调度协调子布局

CoordinatorLayout使用新的思路通过协调调度子布局的形式实现触摸影响布局的形式产生动画效果。CoordinatorLayout通过设置子View的 Behaviors来调度子View。系统(Support V7)提供了AppBarLayout.Behavior, AppBarLayout.ScrollingViewBehavior, FloatingActionButton.Behavior, SwipeDismissBehavior 等。

使用CoordinatorLayout需要在Gradle加入Support Design Library:

  1. compile 'com.android.support:design:22.2.1'
复制代码

具体的怎么协调子控件的,建议大家看下面的参考文章,写的非常好,看完你就基本明白了:
https://segmentfault.com/a/1190000005024216?utm_source=Weibo&utm_medium=shareLink&utm_campaign=socialShare&from=singlemessage&isappinstalled=0

AppBarLayout的介绍

AppBarLayout 是一个竖直排列的线性布局,它实现了很多Material Design风格app bar的设计概念,换句话说就是滚动手势。

在AppBarLayout里面的View,通过app:layout_scrollFlags属性来控制,滚动时候的表现。其中有4种Flag的类型。

scroll: this flag should be set for all views that want to scroll off the screen - for views that do not use this flag, they’ll remain pinned to the top of the screen

enterAlways: this flag ensures that any downward scroll will cause this view to become visible, enabling the ‘quick return’ pattern

enterAlwaysCollapsed: When your view has declared a minHeight and you use this flag, your View will only enter at its minimum height (i.e., ‘collapsed’), only re-expanding to its full height when the scrolling view has reached it’s top.

exitUntilCollapsed: this flag causes the view to scroll off until it is ‘collapsed’ (its minHeight) before exiting

我们的例子中用的是 scroll 和 exitUntilCollapsed。

翻译的比较烂,英文好的,看上面的英文解释。

Scroll: 表示向下滚动时,这个View会被滚出屏幕范围直到隐藏.

enterAlways: 表示向上滚动时,这个View会随着滚动手势出现,直到恢复原来的位置.

enterAlwaysCollapsed: 顾名思义,这个flag定义的是何时进入(已经消失之后何时再次显示)。假设你定义了一个最小高度(minHeight)同时enterAlways也定义了,那么view将在到达这个最小高度的时候开始显示,并且从这个时候开始慢慢展开,当滚动到顶部的时候展开完。

exitUntilCollapsed: 同样顾名思义,这个flag时定义何时退出,当你定义了一个minHeight,这个view将在滚动到达这个最小高度的时候消失。

所以我们就在AppBarLayout里面的CollapsingToolbarLayout进行了如下设置:

  1. <android.support.design.widget.CollapsingToolbarLayout
  2. android:id="@+id/collapsing_toolbar_layout"
  3. android:layout_width="match_parent"
  4. android:layout_height="280dp"
  5. android:fitsSystemWindows="true"
  6. app:collapsedTitleTextAppearance="@style/ToolBarTitleText"
  7. app:contentScrim="#46a8ba"
  8. app:expandedTitleMarginEnd="48dp"
  9. app:expandedTitleMarginStart="48dp"
  10. app:expandedTitleTextAppearance="@style/transparentText"
  11. app:layout_scrollFlags="scroll|exitUntilCollapsed">
  12. </android.support.design.widget.CollapsingToolbarLayout>
复制代码

记住:我们刚才上面也说了AppBarLayout是一个竖直方向的线性布局,如果里面包含多个子View时,要想有折叠动画效果,必须把带有scroll flag的view放在前面,这样收回的view才能让正常退出,而固定的view继续留在顶部。

CollapsingToolbarLayout的介绍

CollapsingToolbarLayout作用是提供了一个可以折叠的Toolbar,它继承至FrameLayout,给它设置layout_scrollFlags,它可以控制包含在CollapsingToolbarLayout中的控件在响应layout_behavior事件时作出相应的scrollFlags滚动事件(移除屏幕或固定在屏幕顶端)。它是设计用于直接AppBarLayout的子视图。

CollapsingToolbarLayout的子View中可以设置这两个属性

ayout_collapseMode (折叠模式) - 有两个值:

pin - 设置为这个模式时,当CollapsingToolbarLayout完全收缩后,Toolbar还可以保留在屏幕上。

parallax - 设置为这个模式时,在内容滚动时,CollapsingToolbarLayout中的View(比如ImageView)也可以同时滚动,实现视差滚动效果,通常和layout_collapseParallaxMultiplier(设置视差因子)搭配使用。

layout_collapseParallaxMultiplier(视差因子) - 设置视差滚动因子,值为:0~1。

关于CollapsingToolbarLayout几个属性的介绍

app:collapsedTitleTextAppearance 这是在收缩时Title文字特点外形的设置

app:expandedTitleTextAppearance 同理这是在展开时Title文字特点外形的设置

app:contentScrim 这是toolbar 标题工具栏停留在顶部时候背景的设置

app:expandedTitleMarginStart 设置扩张时候(还没有收缩时)title向左填充的距离

app:expandedTitleMarginEnd 这个同理是收缩结束时向左填空的距离

其他的就不一一介绍了,具体的去查看API文档即可获知。

我在做这里的时候遇到一个问题,那就是CollapsingToolbarLayout里的Title的问题,一般默认是显示的,即使你不写,它也有会一个默认值一直显示在那里,等折叠收缩完的时候,停留在标题工具栏上。怎么消除这个默认值呢?怎么知道收缩完成了,再把这个值设置出来呢?这里我对AppBarLayout设置了一个监听,它有一个监听方法:addOnOffsetChangedListener监听折叠收缩的位移。如下:

  1. app_bar_layout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
  2. @Override
  3. public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
  4. if (verticalOffset <= -head_layout.getHeight() / 2) {
  5. mCollapsingToolbarLayout.setTitle("涩郎");
  6. } else {
  7. mCollapsingToolbarLayout.setTitle(" ");
  8. }
  9. }
  10. });
复制代码
Toolbar的介绍

Toolbar 是在 Android 5.0 开始推出的一个 Material Design 风格的导航控件 ,Google 非常推荐大家使用 Toolbar 来作为Android客户端的导航栏,以此来取代之前的 Actionbar 。与 Actionbar 相比,Toolbar 明显要灵活的多。它不像 Actionbar 一样,一定要固定在Activity的顶部,而是可以放到界面的任意位置。除此之外,在设计 Toolbar 的时候,Google也留给了开发者很多可定制修改的余地,这些可定制修改的属性在API文档中都有详细介绍,如:

设置导航栏图标;

设置App的logo;

支持设置标题和子标题;

支持添加一个或多个的自定义控件;

支持Action Menu;

Toolbar的具体使用方法,我在这里就不过多的赘述了,学习的点太多了,简单介绍完了,我给大家推荐两篇参考学习使用的文章就行了,写的很详细和完整,之前我们公众号也推送过D_clock写的文章。
学习参考文章:

http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1118/2006.html

http://www.jianshu.com/p/79604c3ddcae#rd

TabLayout的介绍

毫无疑问,TabLayout也是Material Design设计风格,当然也是5.0以后出来的。TabLayout提供一个水平布局来显示选项卡。TabLayout一般都是配合ViewPager一起来使用。TabLayout设置Tab标签有两种方法如下:

第一种
  1. TabLayout tabLayout = ...;
  2. tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
  3. tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
  4. tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
复制代码
第二种
  1. <android.support.design.widget.TabLayout
  2. android:layout_height="wrap_content"
  3. android:layout_width="match_parent">
  4. <android.support.design.widget.TabItem
  5. android:text="@string/tab_text"/>
  6. <android.support.design.widget.TabItem
  7. android:icon="@drawable/ic_android"/>
  8. </android.support.design.widget.TabLayout>
复制代码
TabLayout的坑

使用TabLayout有个坑,这个坑如果一般用户不知道,解决起来比较麻烦,当然看到这篇文章的人有福了,因为你找到解决方法。
如果设计的需求不要求选项卡在切换时附带有图标的切换效果,仅仅文字的颜色发生变化以响应用户的点击事件,那么TabLayout和ViewPager建立联系可以用官方提供的方法,它可以做到交互双向联动,也就是点击tab,viewpager就会去变动,滑动viewpager,tab也会自动变。相互建立联系的方法如下:

  1. setupWithViewPager(ViewPager viewPager)
复制代码

如果选项卡里带有图标或者仅仅只有图标时就麻烦了,那个选项卡会变得什么都没有了。解决方法其实很简单就是不使用上面的方法,而且这样用:

  1. viewPager.addOnPageChangeListener(new TabLayoutOnPageChangeListener(tabLayout));
  2. tabLayout.setOnTabSelectedListener(new ViewPagerOnTabSelectedListener(viewPager));
复制代码

其实这个setupWithViewPager(ViewPager viewPager)方法,跟进到源码里,你可以看到,就是调用了上面的两种方式。

全部代码 布局代码

到这里基本就讲完了,全部的布局代码如下:

  1. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:fitsSystemWindows="true"
  7. tools:context=".MainActivity">
  8. <android.support.design.widget.AppBarLayout
  9. android:id="@+id/app_bar_layout"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:fitsSystemWindows="true">
  13. <android.support.design.widget.CollapsingToolbarLayout
  14. android:id="@+id/collapsing_toolbar_layout"
  15. android:layout_width="match_parent"
  16. android:layout_height="280dp"
  17. android:fitsSystemWindows="true"
  18. app:collapsedTitleTextAppearance="@style/ToolBarTitleText"
  19. app:contentScrim="#46a8ba"
  20. app:expandedTitleMarginEnd="48dp"
  21. app:expandedTitleMarginStart="48dp"
  22. app:expandedTitleTextAppearance="@style/transparentText"
  23. app:layout_scrollFlags="scroll|exitUntilCollapsed">
  24. <LinearLayout
  25. android:id="@+id/head_layout"
  26. android:layout_width="match_parent"
  27. android:layout_height="match_parent"
  28. android:orientation="vertical"
  29. app:layout_collapseMode="pin"
  30. app:layout_collapseParallaxMultiplier="0.7">
  31. <RelativeLayout
  32. android:layout_width="match_parent"
  33. android:layout_height="wrap_content"
  34. android:layout_marginTop="50dp"
  35. android:padding="20dp">
  36. <LinearLayout
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:layout_alignParentLeft="true"
  40. android:layout_centerVertical="true"
  41. android:orientation="vertical">
  42. <TextView
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:text="涩郎"
  46. android:textColor="#ffffff"
  47. android:textSize="16sp"
  48. android:textStyle="bold" />
  49. <TextView
  50. android:layout_width="wrap_content"
  51. android:layout_height="wrap_content"
  52. android:layout_marginTop="3dp"
  53. android:text="android开发工程师 @ 非著名程序员"
  54. android:textColor="#ffffff"
  55. android:textSize="13sp"
  56. android:textStyle="bold" />
  57. </LinearLayout>
  58. <ImageView
  59. android:layout_width="50dp"
  60. android:layout_height="50dp"
  61. android:layout_alignParentRight="true"
  62. android:layout_centerVertical="true"
  63. android:scaleType="centerCrop"
  64. android:src="@mipmap/bg" />
  65. </RelativeLayout>
  66. <TextView
  67. android:layout_width="match_parent"
  68. android:layout_height="wrap_content"
  69. android:layout_marginLeft="20dp"
  70. android:layout_marginRight="40dp"
  71. android:ellipsize="end"
  72. android:maxLines="2"
  73. android:text="关注我(微博@涩郎,微信公众号:非著名程序员),我与你闲扯技术大话,笑谈科技人生。以幽默诙谐的态度,面对乏味无聊的技术,用扯淡的方式,分享技术的内涵。谈的是技术,更是我们的人生。"
  74. android:textColor="#ffffff"
  75. android:textSize="12sp" />
  76. <RelativeLayout
  77. android:layout_width="match_parent"
  78. android:layout_height="wrap_content"
  79. android:layout_marginTop="10dp"
  80. android:padding="20dp">
  81. <LinearLayout
  82. android:id="@+id/one"
  83. android:layout_width="wrap_content"
  84. android:layout_height="wrap_content"
  85. android:layout_alignParentLeft="true"
  86. android:layout_centerVertical="true"
  87. android:orientation="vertical">
  88. <TextView
  89. android:layout_width="wrap_content"
  90. android:layout_height="wrap_content"
  91. android:text="分享"
  92. android:textColor="#ffffff"
  93. android:textSize="12sp"
  94. android:textStyle="bold" />
  95. <TextView
  96. android:layout_width="wrap_content"
  97. android:layout_height="wrap_content"
  98. android:text="50篇"
  99. android:textColor="#ffffff"
  100. android:textSize="12sp"
  101. android:textStyle="bold" />
  102. </LinearLayout>
  103. <LinearLayout
  104. android:id="@+id/two"
  105. android:layout_width="wrap_content"
  106. android:layout_height="wrap_content"
  107. android:layout_centerVertical="true"
  108. android:layout_marginLeft="10dp"
  109. android:layout_toRightOf="@id/one"
  110. android:orientation="vertical">
  111. <TextView
  112. android:layout_width="wrap_content"
  113. android:layout_height="wrap_content"
  114. android:text="获得收藏"
  115. android:textColor="#ffffff"
  116. android:textSize="12sp"
  117. android:textStyle="bold" />
  118. <TextView
  119. android:layout_width="wrap_content"
  120. android:layout_height="wrap_content"
  121. android:text="3000次"
  122. android:textColor="#ffffff"
  123. android:textSize="12sp"
  124. android:textStyle="bold" />
  125. </LinearLayout>
  126. <LinearLayout
  127. android:layout_width="wrap_content"
  128. android:layout_height="wrap_content"
  129. android:layout_centerVertical="true"
  130. android:layout_marginLeft="10dp"
  131. android:layout_toRightOf="@id/two"
  132. android:orientation="vertical">
  133. <TextView
  134. android:layout_width="wrap_content"
  135. android:layout_height="wrap_content"
  136. android:text="被阅读"
  137. android:textColor="#ffffff"
  138. android:textSize="12sp"
  139. android:textStyle="bold" />
  140. <TextView
  141. android:layout_width="wrap_content"
  142. android:layout_height="wrap_content"
  143. android:text="60000次"
  144. android:textColor="#ffffff"
  145. android:textSize="12sp"
  146. android:textStyle="bold" />
  147. </LinearLayout>
  148. <TextView
  149. android:layout_width="wrap_content"
  150. android:layout_height="wrap_content"
  151. android:layout_alignParentRight="true"
  152. android:layout_centerVertical="true"
  153. android:background="@drawable/setting_bg"
  154. android:paddingBottom="3dp"
  155. android:paddingLeft="6dp"
  156. android:paddingRight="6dp"
  157. android:paddingTop="3dp"
  158. android:text="设置"
  159. android:textColor="#ffffff"
  160. android:textSize="12sp" />
  161. </RelativeLayout>
  162. </LinearLayout>
  163. <android.support.v7.widget.Toolbar
  164. android:id="@+id/toolbar"
  165. android:layout_width="match_parent"
  166. android:layout_height="?attr/actionBarSize"
  167. app:layout_collapseMode="pin"
  168. app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
  169. app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
  170. </android.support.design.widget.CollapsingToolbarLayout>
  171. <android.support.design.widget.TabLayout
  172. android:id="@+id/toolbar_tab"
  173. android:layout_width="match_parent"
  174. android:layout_height="?attr/actionBarSize"
  175. android:layout_gravity="bottom"
  176. android:background="#ffffff"
  177. android:fillViewport="false"
  178. app:layout_scrollFlags="scroll"
  179. app:tabIndicatorColor="#0835f8"
  180. app:tabIndicatorHeight="2.0dp"
  181. app:tabSelectedTextColor="#0835f8"
  182. app:tabTextColor="#ced0d3">
  183. <android.support.design.widget.TabItem
  184. android:layout_width="match_parent"
  185. android:layout_height="match_parent"
  186. android:icon="@drawable/tab_selector" />
  187. <android.support.design.widget.TabItem
  188. android:layout_width="match_parent"
  189. android:layout_height="match_parent"
  190. android:text="分享" />
  191. <android.support.design.widget.TabItem
  192. android:layout_width="match_parent"
  193. android:layout_height="match_parent"
  194. android:text="收藏" />
  195. <android.support.design.widget.TabItem
  196. android:layout_width="match_parent"
  197. android:layout_height="match_parent"
  198. android:text="关注" />
  199. <android.support.design.widget.TabItem
  200. android:layout_width="match_parent"
  201. android:layout_height="match_parent"
  202. android:text="关注者" />
  203. </android.support.design.widget.TabLayout>
  204. </android.support.design.widget.AppBarLayout>
  205. <android.support.v4.widget.NestedScrollView
  206. android:layout_width="match_parent"
  207. android:layout_height="match_parent"
  208. android:fillViewport="true"
  209. android:scrollbars="none"
  210. app:layout_behavior="@string/appbar_scrolling_view_behavior">
  211. <android.support.v4.view.ViewPager
  212. android:id="@+id/main_vp_container"
  213. android:layout_width="match_parent"
  214. android:layout_height="match_parent"
  215. app:layout_behavior="@string/appbar_scrolling_view_behavior" />
  216. </android.support.v4.widget.NestedScrollView>
  217. </android.support.design.widget.CoordinatorLayout>
复制代码
Activity中的代码
  1. package com.loonggg.coordinatorlayoutdemo;
  2. import android.annotation.SuppressLint;
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. import android.graphics.drawable.BitmapDrawable;
  6. import android.support.design.widget.AppBarLayout;
  7. import android.support.design.widget.CollapsingToolbarLayout;
  8. import android.support.design.widget.TabLayout;
  9. import android.support.v4.view.ViewPager;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.os.Bundle;
  12. import android.support.v7.widget.Toolbar;
  13. import android.view.Menu;
  14. import android.view.MenuItem;
  15. import android.view.View;
  16. import android.widget.LinearLayout;
  17. import android.widget.Toast;
  18. public class MainActivity extends AppCompatActivity {
  19. private LinearLayout head_layout;
  20. private TabLayout toolbar_tab;
  21. private ViewPager main_vp_container;
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_main);
  26. AppBarLayout app_bar_layout = (AppBarLayout) findViewById(R.id.app_bar_layout);
  27. Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
  28. setSupportActionBar(mToolbar);
  29. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  30. mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
  31. @Override
  32. public void onClick(View v) {
  33. onBackPressed();
  34. }
  35. });
  36. head_layout = (LinearLayout) findViewById(R.id.head_layout);
  37. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.bg);
  38. head_layout.setBackgroundDrawable(new BitmapDrawable(BlurUtil.fastblur(this, bitmap, 180)));
  39. //使用CollapsingToolbarLayout必须把title设置到CollapsingToolbarLayout上,设置到Toolbar上则不会显示
  40. final CollapsingToolbarLayout mCollapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout);
  41. mCollapsingToolbarLayout.setContentScrim(new BitmapDrawable(BlurUtil.fastblur(this, bitmap, 180)));
  42. app_bar_layout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
  43. @Override
  44. public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
  45. if (verticalOffset <= -head_layout.getHeight() / 2) {
  46. mCollapsingToolbarLayout.setTitle("涩郎");
  47. } else {
  48. mCollapsingToolbarLayout.setTitle(" ");
  49. }
  50. }
  51. });
  52. Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
  53. @Override
  54. public boolean onMenuItemClick(MenuItem menuItem) {
  55. String msg = "";
  56. switch (menuItem.getItemId()) {
  57. case R.id.webview:
  58. msg += "博客跳转";
  59. break;
  60. case R.id.weibo:
  61. msg += "微博跳转";
  62. break;
  63. case R.id.action_settings:
  64. msg += "设置";
  65. break;
  66. }
  67. if (!msg.equals("")) {
  68. Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
  69. }
  70. return true;
  71. }
  72. };
  73. mToolbar.setOnMenuItemClickListener(onMenuItemClick);
  74. toolbar_tab = (TabLayout) findViewById(R.id.toolbar_tab);
  75. main_vp_container = (ViewPager) findViewById(R.id.main_vp_container);
  76. ViewPagerAdapter vpAdapter = new ViewPagerAdapter(getSupportFragmentManager(), this);
  77. main_vp_container.setAdapter(vpAdapter);
  78. main_vp_container.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(toolbar_tab));
  79. toolbar_tab.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(main_vp_container));
  80. //tablayout和viewpager建立联系为什么不用下面这个方法呢?自己去研究一下,可能收获更多
  81. //toolbar_tab.setupWithViewPager(main_vp_container);
  82. }
  83. @Override
  84. public boolean onCreateOptionsMenu(Menu menu) {
  85. getMenuInflater().inflate(R.menu.menu_main, menu);
  86. return true;
  87. }
  88. }
复制代码

源码下载地址:https://github.com/loonggg/CoordinatorLayoutDemo
记得star一下哦。

公众号:非著名程序员(smart_android) 欢迎大家关注,每天一篇原创技术文章。

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-6 16:03

Copyright 2015-2025 djqfx

返回顶部