高級(jí)UI<第四十九篇>:NestedScrolling之標(biāo)題欄優(yōu)化方案

在上一節(jié)中呻征,所演示的最終效果圖如下:

56.gif

這張圖中,頂部標(biāo)題欄是一個(gè)標(biāo)題為"權(quán)威發(fā)布"的文本(TextView),Google官方推出一個(gè)專門做為標(biāo)題的控件饺蔑。

[Toolbar]

Android 5.0之后巍耗,Google推出新的標(biāo)題控件秋麸,舊的標(biāo)題控件ActionBar已被Toolbar替代。

首先看一下以下布局:

<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:orientation="vertical"
        app:elevation="0dp">

        <ImageView
            android:id="@+id/imageview"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:scaleType="center"
            app:layout_scrollFlags="scroll"
            android:src="@mipmap/top_pic" />

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior=".MyBehavior"/>


    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#C49797"
        app:title="我是Toolbar"
        app:titleTextColor="@color/colorPrimary"
        app:navigationIcon="@mipmap/back"
        app:layout_collapseMode="parallax"
        app:layout_behavior=".MyBehavior2"
        app:layout_collapseParallaxMultiplier="0.7"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

在Toolbar控件上添加了MyBehavior2行為炬太,MyBehavior2的代碼請(qǐng)查看上一篇文章

http://www.reibang.com/p/f2eb80a44dd9

效果如下:

57.gif

所以灸蟆,這里將TextView換成Toolbar是沒有任何問題的。

[CollapsingToolbarLayout]

這個(gè)控件可以讓Toolbar具有折疊特效亲族,也是許多App的常用手段炒考。

先貼一下布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:orientation="vertical"
        app:elevation="0dp">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:contentScrim="?attr/colorPrimary"
            app:collapsedTitleTextAppearance="@style/toolbarTitle"
            app:expandedTitleTextAppearance="@style/toolbarTitle"
            app:collapsedTitleGravity="left"
            app:expandedTitleGravity="bottom|center"
            app:title="美團(tuán)外賣"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">


            <ImageView
                android:id="@+id/imageview"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:scaleType="center"
                app:layout_scrollFlags="scroll"
                android:src="@mipmap/top_pic" />

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@android:color/transparent"
                app:title="我是Toolbar"
                app:titleTextColor="@color/colorPrimary"
                app:navigationIcon="@mipmap/back"
                app:layout_collapseMode="pin"
                app:layout_collapseParallaxMultiplier="0.7"/>

        </com.google.android.material.appbar.CollapsingToolbarLayout>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior=".MyBehavior"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

效果如下:

58.gif

總之可缚,完成Toolbar的折疊特效需要的控件是:CoordinatorLayout + AppBarLayout + CollapsingToolbarLayout + Toolbar

另外斋枢,CollapsingToolbarLayout控件有一些屬性需要系統(tǒng)了解一下帘靡,可以在網(wǎng)上查找一些資料:

可折疊式標(biāo)題欄 -- CollapsingToolbarLayout 的屬性

toolbarTitle是文字樣式,定義文字的大小和顏色

<style name="toolbarTitle" >
    <item name="android:textSize">18sp</item>
    <item name="android:textColor">#ffffff</item>
</style>

[本章完...]

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末瓤帚,一起剝皮案震驚了整個(gè)濱河市描姚,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌戈次,老刑警劉巖轩勘,帶你破解...
    沈念sama閱讀 218,858評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異怯邪,居然都是意外死亡绊寻,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門擎颖,熙熙樓的掌柜王于貴愁眉苦臉地迎上來榛斯,“玉大人,你說我怎么就攤上這事搂捧⊥运祝” “怎么了?”我有些...
    開封第一講書人閱讀 165,282評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵允跑,是天一觀的道長(zhǎng)王凑。 經(jīng)常有香客問我,道長(zhǎng)聋丝,這世上最難降的妖魔是什么索烹? 我笑而不...
    開封第一講書人閱讀 58,842評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮弱睦,結(jié)果婚禮上百姓,老公的妹妹穿的比我還像新娘。我一直安慰自己况木,他們只是感情好垒拢,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著火惊,像睡著了一般求类。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上屹耐,一...
    開封第一講書人閱讀 51,679評(píng)論 1 305
  • 那天尸疆,我揣著相機(jī)與錄音,去河邊找鬼。 笑死寿弱,一個(gè)胖子當(dāng)著我的面吹牛犯眠,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播症革,決...
    沈念sama閱讀 40,406評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼阔逼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了地沮?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,311評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤羡亩,失蹤者是張志新(化名)和其女友劉穎摩疑,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體畏铆,經(jīng)...
    沈念sama閱讀 45,767評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡雷袋,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了辞居。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片楷怒。...
    茶點(diǎn)故事閱讀 40,090評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖瓦灶,靈堂內(nèi)的尸體忽然破棺而出鸠删,到底是詐尸還是另有隱情,我是刑警寧澤贼陶,帶...
    沈念sama閱讀 35,785評(píng)論 5 346
  • 正文 年R本政府宣布刃泡,位于F島的核電站,受9級(jí)特大地震影響碉怔,放射性物質(zhì)發(fā)生泄漏烘贴。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評(píng)論 3 331
  • 文/蒙蒙 一撮胧、第九天 我趴在偏房一處隱蔽的房頂上張望桨踪。 院中可真熱鬧,春花似錦芹啥、人聲如沸锻离。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)纳账。三九已至,卻和暖如春捺疼,著一層夾襖步出監(jiān)牢的瞬間疏虫,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評(píng)論 1 271
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留卧秘,地道東北人呢袱。 一個(gè)月前我還...
    沈念sama閱讀 48,298評(píng)論 3 372
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像翅敌,于是被迫代替她去往敵國(guó)和親羞福。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容