版權聲明:本文爲HaiyuKing原創文章,轉載請註明出處!html
CollapsingToolBarLayout是一個做用於ToolBar基礎之上的佈局,它也是由Design Support庫提供的。不過CollapsingToolBarLayout(可摺疊的toolbar佈局)是不能獨立存在的,它在設計的時候就被限定只能做爲AppBarLayout的直接子佈局來使用。而APPBarLayout【垂直方向的LinearLayout】又必須是CoordinatiorLayout【增強版的FrameLayout】的子佈局。android
--摘自《第一行代碼(第2版)》git
CardView適用於實現卡片式佈局效果的重要控件,由appcompat-v7庫提供,實際上CardView也是一個FrameLayout,只是額外提供了圓角和陰影效果,看上去有立體的感受。通常CardView用在ListView的item佈局中,或者單獨一個區域在導航欄下方。github
activity_main.xml佈局文件示意圖:app
注意事項:ide
一、 導入類文件後須要change包名以及從新import R文件路徑工具
二、 Values目錄下的文件(strings.xml、dimens.xml、colors.xml等),若是項目中存在,則複製裏面的內容,不要整個覆蓋佈局
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.why.project.collapsingtoolbarlayoutdemo"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//引入design庫 implementation 'com.android.support:design:27.1.1'
}
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> </resources>
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.why.project.collapsingtoolbarlayoutdemo"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//引入design庫
implementation 'com.android.support:design:27.1.1'
//CardView implementation 'com.android.support:cardview-v7:27.1.1'
}
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <!-- toolbar背景色 --> <color name="nav_bg">#3F51B5</color> <!-- toolbar標題顏色 --> <color name="nav_text_color">#ffffff</color> </resources>
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- toolbar高度 --> <dimen name="nav_height">56dp</dimen> </resources>
<?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:background="#f4f4f4"> <!-- 頂部導航欄 --> <android.support.design.widget.AppBarLayout android:id="@+id/appBar" android:layout_width="match_parent" android:layout_height="250dp" > <!--app:contentScrim設置摺疊時工具欄佈局的顏色,默認contentScrim是colorPrimary的色值 app:statusBarScrim設置摺疊時狀態欄的顏色,statusBarScrim是colorPrimaryDark的色值。--> <!--app:layout_scrollFlags: 設置滾動表現: 一、 Scroll, 表示手指向上滑動的時候,CollapsingToolbarLayout也會向上滾出屏幕而且消失,這個屬性必需要有。 二、 exitUntilCollapsed, 表示這個layout會一直滾動離開屏幕範圍,直到它收折成它的最小高度。再也不移出屏幕--> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/collapsing_toolbar" android:layout_width="match_parent" android:layout_height="match_parent" app:contentScrim="@color/nav_bg" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <!-- 這裏能夠是一張圖片或佈局 --> <!--app:layout_collapseMode="parallax"設置爲這個模式時,在內容滾動時,CollapsingToolbarLayout中的View(好比ImageView)也能夠同時滾動,實現視差滾動效果, 一般和layout_collapseParallaxMultiplier(設置視差因子,值爲0~1)搭配使用。--> <!--給layout_collapseParallaxMultiplier設置的值越大可讓滾動的效果更加明顯。--> <!--android:layout_marginTop="@dimen/nav_height"--> <LinearLayout android:id="@+id/head_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/nav_bg" app:layout_collapseMode="parallax" app:layout_collapseParallaxMultiplier="0.8" android:layout_marginTop="@dimen/nav_height" android:padding="8dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="這裏是自定義的佈局,能夠隨意佈局。須要注意設置android:layout_marginTop的值是toolbar的高度值,不然就會被擋住一部分。\n還有就是ToolBar的title須要在代碼中設置。" android:textColor="#ffffff" android:textSize="16sp" android:textStyle="bold" /> </LinearLayout> <!--app:layout_collapseMode="pin" 在view摺疊的時候Toolbar仍然被固定在屏幕的頂部。--> <!-- 這裏只是放一個Toolbar,不作任何處理 --> <android.support.v7.widget.Toolbar android:id="@+id/toolbar_base" android:layout_width="match_parent" android:layout_height="@dimen/nav_height" android:minHeight="@dimen/nav_height" android:background="@color/nav_bg" app:contentInsetStart="0dp" app:contentInsetStartWithNavigation="0dp" app:layout_collapseMode="pin" /> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> <!--中間滾動區域--> <!--app:layout_behavior="@string/appbar_scrolling_view_behavior"是一個系統字符串,值爲:android.support.design.widget.AppBarLayout$ScrollingViewBehavior的常量值。 惟一的做用是把本身(使用者)放到AppBarLayout的下面。(不能理解爲何叫ScrollingViewBehavior)全部View都能使用這個Behavior。--> <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="wrap_content" app: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:background="#ffffff" android:layout_margin="10dp" app:cardCornerRadius="5dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="卡片式佈局" android:textSize="20sp" android:layout_margin="20dp"/> </android.support.v7.widget.CardView> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:layout_margin="10dp" app:cardCornerRadius="5dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="卡片式佈局" android:textSize="20sp" android:layout_margin="20dp"/> </android.support.v7.widget.CardView> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:layout_margin="10dp" app:cardCornerRadius="5dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="卡片式佈局" android:textSize="20sp" android:layout_margin="20dp"/> </android.support.v7.widget.CardView> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:layout_margin="10dp" app:cardCornerRadius="5dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="卡片式佈局" android:textSize="20sp" android:layout_margin="20dp"/> </android.support.v7.widget.CardView> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:layout_margin="10dp" app:cardCornerRadius="5dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="卡片式佈局" android:textSize="20sp" android:layout_margin="20dp"/> </android.support.v7.widget.CardView> </LinearLayout> </android.support.v4.widget.NestedScrollView> </android.support.design.widget.CoordinatorLayout>
package com.why.project.collapsingtoolbarlayoutdemo; import android.os.Bundle; import android.support.design.widget.AppBarLayout; import android.support.design.widget.CollapsingToolbarLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.widget.LinearLayout; public class MainActivity extends AppCompatActivity { private AppBarLayout appBarLayout; private CollapsingToolbarLayout mCollapsingToolbarLayout; private LinearLayout mHeadlayout; private Toolbar mToolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); initToolBar(); setTitleToCollapsingToolbarLayout(); } private void initViews() { appBarLayout = findViewById(R.id.appBar); mCollapsingToolbarLayout = findViewById(R.id.collapsing_toolbar); mHeadlayout = findViewById(R.id.head_layout); } private void initToolBar() { mToolbar = findViewById(R.id.toolbar_base); setSupportActionBar(mToolbar);//因爲toolbar只是一個普通控件,咱們將ToolBar設置爲ActionBar } /** * 使用CollapsingToolbarLayout必須把title設置到CollapsingToolbarLayout上, * 設置到Toolbar上則不會顯示【備註:實際上是能夠的】 */ private void setTitleToCollapsingToolbarLayout() { appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { if (verticalOffset <= -mHeadlayout.getHeight() * 4 /5) { mCollapsingToolbarLayout.setTitle("個人");//設置標題 //使用下面兩個CollapsingToolbarLayout的方法設置展開透明->摺疊時你想要的顏色 mCollapsingToolbarLayout.setExpandedTitleColor(getResources().getColor(android.R.color.transparent)); mCollapsingToolbarLayout.setCollapsedTitleTextColor(getResources().getColor(R.color.nav_text_color)); } else { mCollapsingToolbarLayout.setTitle(""); } } }); } }
無gradle
CollapsingToolbarLayout用法詳解(簡潔易懂)ui
android新特性:使用CollapsingToolbarLayout實現摺疊效果及問題解決
MaterialDesign系列文章(九)CardView的使用及適配