學(xué)習(xí)使用Material Design控件(四)Android實(shí)現(xiàn)標(biāo)題欄自動(dòng)縮放、放大效果
本文要實(shí)現(xiàn)內(nèi)容移動(dòng)時(shí),標(biāo)題欄自動(dòng)縮放/放大的效果,效果如下:

控件介紹
這次需要用到得新控件比較多,主要有以下幾個(gè):
CoordinatorLayout
組織它的子views之間協(xié)作的一個(gè)Layout,它可以給子View切換提供動(dòng)畫(huà)效果。
AppBarLayout
可以讓包含在其中的控件響應(yīng)被標(biāo)記了ScrollingViewBehavior的View的滾動(dòng)事件
CollapsingToolbarLayout
可以控制包含在CollapsingToolbarLayout其中的控件,在響應(yīng)collapse時(shí)是移除屏幕和固定在最上面
TabLayout
結(jié)合ViewPager,實(shí)現(xiàn)多個(gè)TAB的切換的功能
NestedScrollView
與ScrollView基本相同,不過(guò)包含在NestedScrollView中的控件移動(dòng)時(shí)才能時(shí)AppBarLayout縮放
Layout布局
<?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:fitsSystemWindows=“true”>
<android.support.design.widget.AppBarLayout
android:layout_width=“match_parent”
android:layout_height=“256dp”
android:fitsSystemWindows=“true”
android:theme=“@style/ThemeOverlay.AppCompat.Dark.ActionBar”>
<android.support.design.widget.CollapsingToolbarLayout
android:id=“@+id/collapsing_toolbar”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:fitsSystemWindows=“true”
app:contentScrim=“?attr/colorPrimary”
app:expandedTitleMarginEnd=“64dp”
app:expandedTitleMarginStart=“48dp”
app:layout_scrollFlags=“scroll|exitUntilCollapsed”>
<ImageView
android:id=“@+id/ivImage”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:fitsSystemWindows=“true”
android:scaleType=“centerCrop”
android:src=“@drawable/book1”
app:layout_collapseMode=“parallax”
app:layout_collapseParallaxMultiplier=“0.7” />
<android.support.v7.widget.Toolbar
android:id=“@+id/toolbar”
android:layout_width=“match_parent”
android:layout_height=“?attr/actionBarSize”
app:layout_collapseMode=“pin”
app:popupTheme=“@style/ThemeOverlay.AppCompat.Light” />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”
app:layout_behavior=“@string/appbar_scrolling_view_behavior”>
<android.support.design.widget.TabLayout
android:id=“@+id/sliding_tabs”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
app:tabGravity=“fill”
app:tabMode=“fixed” />
<android.support.v4.view.ViewPager
android:id=“@+id/viewpager”
android:layout_width=“match_parent”
android:layout_height=“match_parent” />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
CollapsingToolbarLayout和TabLayout的使用說(shuō)明可以參考探索新的Android Material Design支持庫(kù)
代碼實(shí)現(xiàn)
//Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onBackPressed();
}
});
//使用CollapsingToolbarLayout后,title需要設(shè)置到CollapsingToolbarLayout上
CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle("失控");
//設(shè)置ViewPager
mViewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(mViewPager);
//給TabLayout增加Tab, 并關(guān)聯(lián)ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.addTab(tabLayout.newTab().setText("內(nèi)容簡(jiǎn)介"));
tabLayout.addTab(tabLayout.newTab().setText("作者簡(jiǎn)介"));
tabLayout.addTab(tabLayout.newTab().setText("目錄"));
tabLayout.setupWithViewPager(mViewPager);
詳細(xì)代碼參見(jiàn)這里
項(xiàng)目源碼已發(fā)布到Github,Material Design新控件基本介紹完了,
下篇文章會(huì)結(jié)合豆瓣讀書(shū)的API,整合一下這些控件,做一個(gè)Demo。
源碼地址:MaterialDesignExample
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開(kāi)發(fā):淺談MVP模式應(yīng)用與內(nèi)存泄漏問(wèn)題解決
本篇文章主要介紹了Android開(kāi)發(fā):MVP模式應(yīng)用與內(nèi)存泄漏問(wèn)題解決,具有一定的參考價(jià)值,有需要的可以了解一下。2016-11-11
淺析Android Service中實(shí)現(xiàn)彈出對(duì)話(huà)框的坑
這篇文章主要介紹了Android Service中實(shí)現(xiàn)彈出對(duì)話(huà)框的坑,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Android sqlite cursor的遍歷實(shí)例詳解
在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于Android sqlite cursor的遍歷的相關(guān)實(shí)例及知識(shí)點(diǎn),需要的朋友們可以學(xué)習(xí)下。2021-06-06
android開(kāi)發(fā)實(shí)現(xiàn)列表控件滾動(dòng)位置精確保存和恢復(fù)的方法(推薦)
下面小編就為大家?guī)?lái)一篇android開(kāi)發(fā)實(shí)現(xiàn)列表控件滾動(dòng)位置精確保存和恢復(fù)的方法(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
Android仿QQ微信實(shí)時(shí)監(jiān)測(cè)網(wǎng)絡(luò)狀態(tài)
這篇文章主要為大家詳細(xì)介紹了Android仿QQ微信實(shí)時(shí)監(jiān)測(cè)網(wǎng)絡(luò)狀態(tài),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android用ActionBar高仿微信主界面的實(shí)例代碼
這篇文章主要介紹了Android用ActionBar高仿微信主界面的實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Android LayoutTransiton實(shí)現(xiàn)簡(jiǎn)單的錄制按鈕
這篇文章主要介紹了Android LayoutTransiton實(shí)現(xiàn)簡(jiǎn)單的錄制按鈕,主要實(shí)現(xiàn)開(kāi)始,暫停,停止和顯示錄制時(shí)間長(zhǎng)度,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Android ExpandableListView使用方法案例詳解
這篇文章主要介紹了Android ExpandableListView使用方法案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08

