项目作者: hanjie511

项目描述 :
Android的material-design(NavigationView、ToolBar、ButtonNavigationView、AppBarLayout、CoordinatorLayout、CollapsingToolbarLayout、SwipeRefreshLayout、FloatingActionButton)的使用方法
高级语言: Java
项目地址: git://github.com/hanjie511/android-material-design1.git
创建时间: 2019-09-17T08:41:54Z
项目社区:https://github.com/hanjie511/android-material-design1

开源协议:Apache License 2.0

下载


android-material-design

Android的material-design(NavigationView、ToolBar、ButtonNavigationView、AppBarLayout、CoordinatorLayout、CollapsingToolbarLayout、SwipeRefreshLayout、FloatingActionButton)的使用方法。

其中还包括了CardView和在toolbar中嵌入SearchView的使用方法。

项目展示

navigationView
buttonNavigationView
loadmore
refresh
searchView

NavigationView

如第一张图片所展示的功能一样,它主要实现了侧滑导航菜单栏。它位于android.support.design.widget包中。

  • show my code:
    1. <android.support.design.widget.NavigationView
    2. android:id="@+id/nav_main"
    3. android:layout_width="match_parent"
    4. android:layout_height="match_parent"
    5. app:headerLayout="@layout/nav_header"
    6. app:itemTextAppearance="@style/nav_menu_text"
    7. app:menu="@menu/navigation_menu"
    8. ></android.support.design.widget.NavigationView>
    app:headerLayout和app:menu属性分别对应其头部布局文件和菜单布局文件

    ToolBar

    用来代替Android原来的ActionBar。Toolbar能够让我们对手机的页面头部的设计更加的快捷,灵活,多变。
  • show my code:
    1. <android.support.v7.widget.Toolbar
    2. android:id="@+id/toolbar"
    3. android:layout_width="match_parent"
    4. android:layout_height="?attr/actionBarSize"
    5. android:paddingTop="10dp"
    6. android:theme="@style/AppTheme1"
    7. app:layout_collapseMode="pin"
    8. app:navigationIcon="@drawable/ic_menu_black_24dp"
    9. app:popupTheme="@style/ThemeOverlay.AppCompat.Light" ></android.support.v7.widget.Toolbar>
    app:navigationIcon用来定义手机页面左上角的导航图标,

    ButtonNavigationView

    如第二张图片所示。它主要实现了手机页面底部的导航栏。它简化了我们原来设计底部导航栏的步骤,它使得我们的开发变得更加的快捷。
  • show my code:
    1. <android.support.design.widget.BottomNavigationView
    2. android:id="@+id/buttonNavigation"
    3. android:layout_width="match_parent"
    4. android:layout_height="?attr/actionBarSize"
    5. android:layout_weight="2"
    6. app:itemIconTint="@color/navi_button_selector"
    7. app:itemTextColor="@color/navi_button_selector"
    8. app:labelVisibilityMode="labeled"
    9. app:menu="@menu/navigatiob_menu" ></android.support.design.widget.BottomNavigationView>
    app:menu属性用来定义它的菜单项,其中包括选项名称和图标。若我们不定义菜单选中时的selector,它会使用其默认的selector。

    SwipeRefreshLayout

    google官方提供的一款下拉刷新的控件,它的使用方法很简单,我们只需要把ListView或RecyclerView作为它的唯一子控件即可,当我们这样设置完成后,如果列表中有数据且我们从列表顶部下拉时,它会自动检测到我们的下拉手势,并且弹出下拉刷新提示框。
  • show my code:

    1. <android.support.v4.widget.SwipeRefreshLayout
    2. android:id="@+id/swipeRefresh"
    3. android:layout_width="match_parent"
    4. android:layout_height="match_parent"
    5. app:layout_behavior="@string/appbar_scrolling_view_behavior">
    6. <android.support.v7.widget.RecyclerView
    7. android:id="@+id/recyclerView"
    8. android:layout_width="match_parent"
    9. android:layout_height="match_parent"
    10. ></android.support.v7.widget.RecyclerView>
    11. </android.support.v4.widget.SwipeRefreshLayout>

    如上,我就将RecyclerView作为SwipeRefreshLayout的唯一子控件。just like this,it’s done!!!

    FloatingActionButton

    就是我们展示页面的那个红色的圆形的图标,故名思意,它是悬浮在手机页面之上的,它和其它的手机控件不在同一个维度。所以,不管它下面的内容如何变化,它都会漂浮在原来的位置上。注意:他必须位于位于FrameLayout性质的布局文件中,不然,它就漂浮不起来了!!!

  • show my code:
    1. <android.support.design.widget.FloatingActionButton
    2. android:id="@+id/fb"
    3. android:layout_width="wrap_content"
    4. android:layout_height="wrap_content"
    5. android:src="@drawable/ic_share_black_24dp"
    6. app:layout_anchor="@id/appBar"
    7. app:layout_anchorGravity="bottom|right" ></android.support.design.widget.FloatingActionButton>

    AppBarLayout、CoordinatorLayout和CollapsingToolbarLayout

    这三个我要一起说,因为他们就好像是爷爷(CoordinatorLayout)、父亲(AppBarLayout)和儿子(CollapsingToolbarLayout)的关系。他们都自能做各自的子控件。如AppBarLayout只能做CoordinatorLayout的子控件,而CollapsingToolbarLayout只能做AppBarLayout的子控件。
  • CoordinatorLayout他是一个加强版的FrameLayout。他凭什么比FrameLayout优秀呢?因为,它能够监测到位于它布局容器内的所有控件的变化,进而做出相应的处理,而FrameLayout不行。
  • AppBarLayout相当于一个垂直布局的LinearLayout,它的内部封装了一些滚动事件,位于它内部的控件都能使用它封装好的滚动事件。使用它的滚动事件的属性为:app:layout_scrollFlags。
  • CollapsingToolbarLayout为可折叠的Toolbar布局,它可以更加丰富我们的Toolbar,它不会让我们的Toolbar只是像一个单调的页面标题栏了。它做到了让ToolBar可伸缩,还做到了让toolbar的布局和内容跟美观和丰富。该布局缩到最小后就是一个ToolBar。
  • show my code:
    1. <android.support.design.widget.CoordinatorLayout
    2. android:layout_width="match_parent"
    3. android:layout_height="match_parent"
    4. android:layout_weight="8"
    5. android:fitsSystemWindows="true">
    6. <android.support.design.widget.AppBarLayout
    7. android:id="@+id/appBar"
    8. android:layout_width="match_parent"
    9. android:layout_height="250dp"
    10. android:fitsSystemWindows="true">
    11. <android.support.design.widget.CollapsingToolbarLayout
    12. android:layout_width="match_parent"
    13. android:layout_height="match_parent"
    14. android:fitsSystemWindows="true"
    15. app:contentScrim="@color/colorPrimary"
    16. app:layout_scrollFlags="scroll|exitUntilCollapsed">
    17. <ImageView
    18. android:layout_width="match_parent"
    19. android:layout_height="match_parent"
    20. android:fitsSystemWindows="true"
    21. android:scaleType="centerCrop"
    22. android:src="@mipmap/header_bg"
    23. app:layout_collapseMode="parallax" ></ImageView>
    24. <android.support.v7.widget.Toolbar
    25. android:id="@+id/toolbar"
    26. android:layout_width="match_parent"
    27. android:layout_height="?attr/actionBarSize"
    28. android:paddingTop="10dp"
    29. android:theme="@style/AppTheme1"
    30. app:layout_collapseMode="pin"
    31. app:navigationIcon="@drawable/ic_menu_black_24dp"
    32. app:popupTheme="@style/ThemeOverlay.AppCompat.Light" ></android.support.v7.widget.Toolbar>
    33. </android.support.design.widget.CollapsingToolbarLayout>
    34. </android.support.design.widget.AppBarLayout>
    35. <android.support.design.widget.FloatingActionButton
    36. android:id="@+id/fb"
    37. android:layout_width="wrap_content"
    38. android:layout_height="wrap_content"
    39. android:src="@drawable/ic_share_black_24dp"
    40. app:layout_anchor="@id/appBar"
    41. app:layout_anchorGravity="bottom|right" ></android.support.design.widget.FloatingActionButton>
    42. <android.support.v4.widget.SwipeRefreshLayout
    43. android:id="@+id/swipeRefresh"
    44. android:layout_width="match_parent"
    45. android:layout_height="match_parent"
    46. <!--app:layout_behavior属性让AppBarLayout明白谁在滚动,当滚动控件滚动时,AppbarLayout好做出相应的变化 -->
    47. app:layout_behavior="@string/appbar_scrolling_view_behavior">
    48. <android.support.v7.widget.RecyclerView
    49. android:id="@+id/recyclerView"
    50. android:layout_width="match_parent"
    51. android:layout_height="match_parent"
    52. </android.support.v4.widget.SwipeRefreshLayout>
    53. </android.support.design.widget.CoordinatorLayout>

    重要的属性介绍:

  • app:layout_collapseMode属性用来设置位于CollapsingToolbarLayout中的控件的折叠模式。它的值有3个,分别是parallax,pin和none.

    pin表示:在CollapsingToolbarLayout折叠的过程中拥有该属性值的控件的大小不变。

    parallax表示:在CollapsingToolbarLayout折叠的过程中拥有该属性值的控件会沿着与x轴平行的方向折叠(即沿着y轴的方向)。

    none表示不进行折叠。
  • app:layout_scrollFlags属性的属性值得介绍:它的属性值常用的有:scroll|exitUntilCollapsed|enterAlwaysCollapsed|enterAlways|snap这五个属性值。

    scroll表示:当滚动视图向上滚动时,带有该属性值的控件会跟着向上滚动并实现隐藏。

    snap表示:带有该属性值的控件在还没有完全显示或隐藏时,它会根据它的滚动距离自定选择时隐藏还是显示。

    enterAlways表示:带有该属性值的控件会跟随着滚动视图一起向下滚动(只要滚动视图向下一滚动,它就开始滚动)。

    exitUntilCollapsed表示:带有该属性值的控件在随着滚动视图向上滚动时,不会退出页面(隐藏)。

    enterAlwaysCollapsed表示:带有该属性值的控件会在滚动视图向下滚动结束以后才会才会开始向下滚动,而不是像设置了enterAlways属性的控件会随着滚动视图一开始就一起滚动。

    show my all code:

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. android:id="@+id/drawer"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. android:orientation="vertical">
    8. <!--主界面的布局控件header -->
    9. <LinearLayout
    10. android:layout_width="match_parent"
    11. android:layout_height="match_parent"
    12. android:orientation="vertical">
    13. <android.support.design.widget.CoordinatorLayout
    14. android:layout_width="match_parent"
    15. android:layout_height="match_parent"
    16. android:layout_weight="8"
    17. android:fitsSystemWindows="true">
    18. <android.support.design.widget.AppBarLayout
    19. android:id="@+id/appBar"
    20. android:layout_width="match_parent"
    21. android:layout_height="250dp"
    22. android:fitsSystemWindows="true">
    23. <android.support.design.widget.CollapsingToolbarLayout
    24. android:layout_width="match_parent"
    25. android:layout_height="match_parent"
    26. android:fitsSystemWindows="true"
    27. app:contentScrim="@color/colorPrimary"
    28. app:layout_scrollFlags="scroll|exitUntilCollapsed">
    29. <ImageView
    30. android:layout_width="match_parent"
    31. android:layout_height="match_parent"
    32. android:fitsSystemWindows="true"
    33. android:scaleType="centerCrop"
    34. android:src="@mipmap/header_bg"
    35. app:layout_collapseMode="parallax" ></ImageView>
    36. <android.support.v7.widget.Toolbar
    37. android:id="@+id/toolbar"
    38. android:layout_width="match_parent"
    39. android:layout_height="?attr/actionBarSize"
    40. android:paddingTop="10dp"
    41. android:theme="@style/AppTheme1"
    42. app:layout_collapseMode="pin"
    43. app:navigationIcon="@drawable/ic_menu_black_24dp"
    44. app:popupTheme="@style/ThemeOverlay.AppCompat.Light" ></android.support.v7.widget.Toolbar>
    45. </android.support.design.widget.CollapsingToolbarLayout>
    46. </android.support.design.widget.AppBarLayout>
    47. <android.support.design.widget.FloatingActionButton
    48. android:id="@+id/fb"
    49. android:layout_width="wrap_content"
    50. android:layout_height="wrap_content"
    51. android:src="@drawable/ic_share_black_24dp"
    52. app:layout_anchor="@id/appBar"
    53. app:layout_anchorGravity="bottom|right" ></android.support.design.widget.FloatingActionButton>
    54. <android.support.v4.widget.SwipeRefreshLayout
    55. android:id="@+id/swipeRefresh"
    56. android:layout_width="match_parent"
    57. android:layout_height="match_parent"
    58. app:layout_behavior="@string/appbar_scrolling_view_behavior">
    59. <android.support.v7.widget.RecyclerView
    60. android:id="@+id/recyclerView"
    61. android:layout_width="match_parent"
    62. android:layout_height="match_parent"
    63. ></android.support.v7.widget.RecyclerView>
    64. </android.support.v4.widget.SwipeRefreshLayout>
    65. </android.support.design.widget.CoordinatorLayout>
    66. <android.support.design.widget.BottomNavigationView
    67. android:id="@+id/buttonNavigation"
    68. android:layout_width="match_parent"
    69. android:layout_height="?attr/actionBarSize"
    70. android:layout_weight="2"
    71. app:itemIconTint="@color/navi_button_selector"
    72. app:itemTextColor="@color/navi_button_selector"
    73. app:labelVisibilityMode="labeled"
    74. app:menu="@menu/navigatiob_menu" ></android.support.design.widget.BottomNavigationView>
    75. </LinearLayout>
    76. <!--主界面的布局控件footer -->
    77. <!-- 侧滑界面的布局header-->
    78. <!-- 注意:侧滑界面的布局必须位于主界面布局的下方,否则会造成“点击侧滑菜单内容后,侧滑菜单消失的情况”-->
    79. <LinearLayout
    80. android:layout_width="match_parent"
    81. android:layout_height="match_parent"
    82. android:layout_gravity="left"
    83. android:orientation="vertical">
    84. <android.support.design.widget.NavigationView
    85. android:id="@+id/nav_main"
    86. android:layout_width="match_parent"
    87. android:layout_height="match_parent"
    88. app:headerLayout="@layout/nav_header"
    89. app:itemTextAppearance="@style/nav_menu_text"
    90. app:menu="@menu/navigation_menu">
    91. <!-- drawerLayout页面的底部按钮header-->
    92. <LinearLayout
    93. android:layout_width="match_parent"
    94. android:layout_height="wrap_content"
    95. android:layout_gravity="bottom"
    96. android:orientation="vertical">
    97. <LinearLayout
    98. android:layout_width="match_parent"
    99. android:layout_height="wrap_content"
    100. android:orientation="horizontal">
    101. <Button
    102. android:id="@+id/nav_reLogin"
    103. android:layout_width="0dp"
    104. android:layout_height="wrap_content"
    105. android:layout_weight="1"
    106. android:background="@android:color/transparent"
    107. android:onClick="reLogin"
    108. android:text="重新登录" ></Button>
    109. <Button
    110. android:id="@+id/nav_exit"
    111. android:layout_width="0dp"
    112. android:layout_height="wrap_content"
    113. android:layout_weight="1"
    114. android:background="@android:color/transparent"
    115. android:onClick="exit"
    116. android:text="退出" ></Button>
    117. </LinearLayout>
    118. <View
    119. android:layout_width="match_parent"
    120. android:layout_height="1dp"
    121. android:background="@android:color/darker_gray" ></View>
    122. </LinearLayout>
    123. <!-- drawerLayout页面的底部按钮footer-->
    124. </android.support.design.widget.NavigationView>
    125. </LinearLayout>
    126. <!-- 侧滑界面的布局footer-->
    127. </android.support.v4.widget.DrawerLayout>