廢話(huà)不多說(shuō)? 先看看這東西的效果圖。
一張沒(méi)圖片的和一張有圖片的,圖片太丑別怪我蟹演。還是把圖換了吧电抚,怕你們打我。
開(kāi)始無(wú)知的我還以為是監(jiān)聽(tīng) onTouchListener 根據(jù)滑動(dòng)的距離和位置來(lái)改變顯示效果來(lái)實(shí)現(xiàn)的。
唉,今天無(wú)意中看到這個(gè)?CollapsingToolbarLayout 。
百度了一下跋核,大致了解點(diǎn)。
不過(guò)東西還是要到 官方的地盤(pán)去看看原版的叛买。
所以就去android.support.design.widget這個(gè)包下找到“元兇”CollapsingToolbarLayout了砂代。
然后順便揪出了這些東西分享給大家。
1.android.support.design:collapsedTitleGravity
e.g. ?app:collapsedTitleGravity="left"
Specifies how the title should be positioned when collapsed.
指定在折疊之后標(biāo)題放置的位置
2.android.support.design:collapsedTitleTextAppearance
e.g. ?app:collapsedTitleTextAppearance="@color/colorPrimary"
The text appearance of the CollapsingToolbarLayouts title when it is fully 'collapsed'
Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".
在折疊的時(shí)候標(biāo)題文字的外觀必須引用另一個(gè)資源 聪全,以“@[+][包:]類(lèi)型:名稱(chēng)”或 主題屬性形式”?(包:)類(lèi)型:名稱(chēng)”泊藕。
2.1 ?android.support.design:expandedTitleTextAppearance
和上面的一樣 ?在展開(kāi)的時(shí)候標(biāo)題文字的外觀
3.android.support.design:contentScrim
e.g. ?app:contentScrim="#ff5252"
The drawable to use as a scrim on top of the CollapsingToolbarLayouts content when it has been scrolled sufficiently off screen.
指定在折疊之后的背景色
4.android.support.design:expandedTitleGravity
e.g. ?app:expandedTitleGravity="left|bottom"
Specifies how the title should be positioned when expanded.
在展開(kāi)的時(shí)候 標(biāo)題放置的位置
5.android.support.design:expandedTitleMargin
e.g. ?app:expandedTitleMargin="16dp"
Specifies extra space on the start, top, end and bottom sides of the the expanded title text.
設(shè)置邊界距離 和 textview等效果一樣
6.android.support.design:expandedTitleMarginBottom底部的margin距離
7.android.support.design:expandedTitleMarginEnd右邊的margin距離
8.android.support.design:expandedTitleMarginStart左邊的margin距離
9.android.support.design:statusBarScrim
e.g. ?app:statusBarScrim="#123456"
The drawable to use as a scrim for the status bar content when the CollapsingToolbarLayout has been scrolled sufficiently off screen.
在折疊的時(shí)候 狀態(tài)欄的背景顏色
10. android.support.design:title
e.g. ?app:title="Title"
The title to show when titleEnabled is set to true.
如果標(biāo)題可用的話(huà) 顯示的標(biāo)題文字
11.android.support.design:titleEnabled
e.g. ?app:titleEnabled="true"
Whether the CollapsingToolbarLayout should draw its own shrinking/growing title.
是否顯示標(biāo)題
12.android.support.design:toolbarId
e.g. ?app:toolbarId="@id/toolbar"
The id of the primary Toolbar child that you wish to use for the purpose of collapsing.
在折疊的時(shí)候 顯示的toolbar的id
13.app:layout_scrollFlags="scroll|exitUntilCollapsed"
scroll - 想滾動(dòng)就必須設(shè)置這個(gè)。
enterAlways - 實(shí)現(xiàn)quick return效果, 當(dāng)向下移動(dòng)時(shí)难礼,立即顯示View(比如Toolbar)娃圆。
exitUntilCollapsed - 向上滾動(dòng)時(shí)收縮View,但可以固定Toolbar一直在上面蛾茉。
enterAlwaysCollapsed - 當(dāng)你的View已經(jīng)設(shè)置minHeight屬性又使用此標(biāo)志時(shí)讼呢,你的View只能以最小高度進(jìn)入,只有當(dāng)滾動(dòng)視圖到達(dá)頂部時(shí)才擴(kuò)大到完整高度谦炬。
測(cè)試的xml文件悦屏,可以copy進(jìn)去測(cè)試就行。
一個(gè)問(wèn)題NestedScrollView高度加上頂部AppBarLayout高度沒(méi)超過(guò)屏幕高低滑動(dòng)有問(wèn)題(只能在AppBarLayout范圍內(nèi)才能滑動(dòng))键思;
這里沒(méi)換MarkDown? 代碼不好弄, ?代碼傳送門(mén)
里面的有個(gè)imageview础爬,注釋已經(jīng)寫(xiě)的很清楚了。
當(dāng)然 里面的toolbar 你們還可以設(shè)置其他的屬性
然后里面的NestedScrollView這貨吼鳞。
它和scrollview 基本上一樣 ?但是它支持嵌套滾動(dòng) ?嵌套滾動(dòng)默認(rèn)是啟用的看蚜。
還有一個(gè)屬性,不過(guò)蛋疼的是這屬性要全手打赔桌,沒(méi)提示不知道怎么破供炎。
android:fillViewportlike: android:fillViewport="true"
Defines whether the scrollview should stretch its content to fill the viewport.
定義滾動(dòng)視圖是否應(yīng)該伸展它的內(nèi)容來(lái)填補(bǔ)視窗渴逻。
然后就是這個(gè)類(lèi)的一些方法:
draw(Canvas canvas)
Manually render this view (and all of its children) to the given Canvas.
渲染指定的畫(huà)布
//獲取新的布局參數(shù)
generateLayoutParams(AttributeSet attrs)
Returns a new set of layout parameters based on the supplied attributes set.
//設(shè)置和獲取折疊之后的標(biāo)題位置
getCollapsedTitleGravity();setCollapsedTitleGravity(int gravity)
//獲取和設(shè)置折疊之后的背景
getContentScrim()
setContentScrim(Drawable drawable)
setContentScrimColor(int color)
setContentScrimResource(int resId)
//設(shè)置和獲取展開(kāi)的標(biāo)題位置
setExpandedTitleGravity(int gravity)
getExpandedTitleGravity()
//獲取和設(shè)置狀態(tài)欄的顏色
getStatusBarScrim()
setStatusBarScrim(Drawable drawable)
setStatusBarScrimColor(int color)
setStatusBarScrimResource(int resId)
//設(shè)置和獲取標(biāo)題
setTitle(CharSequence title)
getTitle()
//獲取和設(shè)置標(biāo)題文字是否顯示
isTitleEnabled()
setTitleEnabled(boolean enabled)
//設(shè)置折疊之后的標(biāo)題文字外觀
setCollapsedTitleTextAppearance(int resId)
//設(shè)置折疊之后的標(biāo)題文字顏色
setCollapsedTitleTextColor(int color)
//設(shè)置展開(kāi)的標(biāo)題文字顏色
setExpandedTitleColor(int color)
//設(shè)置展開(kāi)的標(biāo)題文字外觀
setExpandedTitleTextAppearance(int resId)
如果你喜歡我寫(xiě)的,請(qǐng)關(guān)注我音诫。
歡迎留言拍磚惨奕。
我的博客:http://blog.csdn.net/lxk_1993
友情鏈接;