




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第Android實(shí)現(xiàn)可折疊式標(biāo)題欄本文實(shí)例為大家分享了Android實(shí)現(xiàn)可折疊式標(biāo)題欄的具體代碼,供大家參考,具體內(nèi)容如下
先看效果圖:
一、實(shí)現(xiàn)步驟:
1、布局文件
xmlversion="1.0"encoding="utf-8"
androidx.coordinatorlayout.widget.CoordinatorLayoutxmlns:android="/apk/res/android"
xmlns:app="/apk/res-auto"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.FruitActivity"
com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="250dp"
com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsing"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:contentScrim="attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
ImageView
android:id="@+id/iv_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/head"
app:layout_collapseMode="parallax"/
androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="attr/actionBarSize"
app:layout_collapseMode="pin"/
/com.google.android.material.appbar.CollapsingToolbarLayout
/com.google.android.material.appbar.AppBarLayout
androidx.core.widget.NestedScrollView
android:id="@+id/nested_scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="35dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
app:cardBackgroundColor="@color/white"
app:cardCornerRadius="4dp"
TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="我這里是一個(gè)卡片布局!"/
/androidx.cardview.widget.CardView
/LinearLayout
/androidx.core.widget.NestedScrollView
com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:background="#00000000"
android:src="@drawable/comment"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end"/com.google.android.material.floatingactionbutton.FloatingActionButton
/androidx.coordinatorlayout.widget.CoordinatorLayout
接下來(lái)我們來(lái)分析這里面的控件和屬性:
1、最外層的布局為CoordinatorLayout
:相當(dāng)于加強(qiáng)版的FrameLayout,在普通情況下的作用和FrameLayout基本一致。當(dāng)然也會(huì)有其獨(dú)特的作用,CoordinatorLayout可以監(jiān)聽(tīng)其所有子控件的各種事件,然后自動(dòng)幫我們做出最為合理的響應(yīng)。
2、AppBarLayout:實(shí)際上是一個(gè)垂直方向的LinearLayout,在內(nèi)部做了很多封裝,并應(yīng)用了一些MaterialDesign的設(shè)計(jì)理念。
3、CollapsingToolbarLayout是作用于Toolbar基礎(chǔ)之上的一個(gè)布局,CollapsingToolbarLayout可以讓Toolbar的效果變得更加豐富。
4、app:layout_scrollFlags=scroll|exitUntilCollapsed屬性:srcoll表示CollapsingToolbarLayout會(huì)隨著內(nèi)容的滾動(dòng)一起滾動(dòng),exitUntilCollapsed表示當(dāng)CollapsingToolbarLayout隨著滾動(dòng)完成折疊之后就保留在界面上,不再移出屏幕。
5、app:contentScrim=attr/colorPrimary屬性:用于指定在CollapsingToolbarLayout在趨于折疊狀態(tài)以及折疊之后的背景色。
6、app:layout_collapseMode=pin屬性:用于指定在控件CollapsingToolbarLayout折疊過(guò)程中的折疊模式,pin表示在折疊過(guò)程中位置始終不變。
7、app:layout_collapseMode=parallax屬性:表示在折疊的過(guò)程中產(chǎn)生一定的錯(cuò)位偏移。
8、NestedScrollView控件:即有ScrollView控件使用滾動(dòng)的方式來(lái)查看屏幕以外的數(shù)據(jù),NestedScrollView在此基礎(chǔ)之上還增加了嵌套響應(yīng)滾動(dòng)事件的功能。
9、app:layout_behavior=@string/appbar_scrolling_view_behavior指定了一個(gè)布局行為
10、CardView:用于實(shí)現(xiàn)卡片式布局效果的重要控件,額外提供了圓角和陰影的效果。
11、app:cardCornerRadius屬性:指定卡片圓角的弧度。
12、FloatingActionButton懸浮按鈕
關(guān)于控件和屬性就說(shuō)這么多。
接下來(lái)就是實(shí)現(xiàn)java代碼了,代碼如下:
publicclassFruitActivityextendsAppCompatActivity{
privateCollapsingToolbarLayoutcollapsing;
privateToolbartoolbar;
privateFloatingActionButtonfloating;
privateTextViewtv_text;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fruit);
collapsing=findViewById(R.id.collapsing);
toolbar=findViewById(R.id.toolbar);
floating=findViewById(R.id.floating);
tv_text=findViewById(R.id.tv_text);
setSupportActionBar(toolbar);
ActionBaractionBar=getSupportActionBar();
if(actionBar!=null){
actionBar.setDisplayHomeAsUpEnabled(true);
}
collapsing.setTitle("這是CollapsingToolbarLayout");
Stringtext="努力努力再努力";
tv_text.setText(generateText(text));
floating.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
Toast.makeText(FruitActivity.this,"您點(diǎn)擊了懸浮按鈕哦!",Toast.LENGTH_SHORT).show();
}
});
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 糧食產(chǎn)業(yè)鏈韌性及其空間演化規(guī)律的研究
- 中國(guó)古代詩(shī)詞欣賞與解讀課程教案
- 持續(xù)評(píng)估與反饋機(jī)制在集團(tuán)化辦學(xué)效提升中的作用
- 雪兒作文500字(11篇)
- 高技術(shù)行業(yè)對(duì)就業(yè)市場(chǎng)的影響與挑戰(zhàn)
- 全球能源轉(zhuǎn)型趨勢(shì)對(duì)學(xué)科布局的影響
- 提升中醫(yī)醫(yī)院服務(wù)能力的設(shè)施改造方案
- 2025年小學(xué)語(yǔ)文期末考試試題及答案
- 2025年歷史文化與現(xiàn)代文明的交融能力綜合考試試卷及答案
- 2025年能源與動(dòng)力工程師考試試題及答案
- 第五單元《面積》(教學(xué)設(shè)計(jì))-【大單元教學(xué)】三年級(jí)數(shù)學(xué)下冊(cè)同步備課系列(人教版)
- 閱讀認(rèn)知策略的跨學(xué)科研究框架構(gòu)建
- 摜蛋考試試題及答案
- GA/T 2159-2024法庭科學(xué)資金數(shù)據(jù)清洗規(guī)程
- DB63-T 2129-2023 鹽湖資源開(kāi)發(fā)標(biāo)準(zhǔn)體系
- 企業(yè)風(fēng)險(xiǎn)管理-戰(zhàn)略與績(jī)效整合(中文版-雷澤佳譯)
- 業(yè)務(wù)學(xué)習(xí)踝關(guān)節(jié)骨折
- 實(shí)景演出制作合同協(xié)議
- 迅鐳激光切割機(jī)操作培訓(xùn)
- JJF 2241-2025電子停車(chē)計(jì)時(shí)收費(fèi)表校準(zhǔn)規(guī)范
- 人文關(guān)懷示范病房工作分享課件
評(píng)論
0/150
提交評(píng)論