亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android實現(xiàn)可折疊式標題欄

 更新時間:2022年09月19日 14:48:45   作者:路宇  
這篇文章主要為大家詳細介紹了Android實現(xiàn)可折疊式標題欄,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android實現(xiàn)可折疊式標題欄的具體代碼,供大家參考,具體內(nèi)容如下

先看效果圖:

一、實現(xiàn)步驟:

1、布局文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? xmlns:app="http://schemas.android.com/apk/res-auto"
? ? xmlns:tools="http://schemas.android.com/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="我這里是一個卡片布局!" />
? ? ? ? ? ? </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>

接下來我們來分析這里面的控件和屬性:

1、最外層的布局為 CoordinatorLayout
:相當于加強版的FrameLayout,在普通情況下的作用和FrameLayout基本一致。當然也會有其獨特的作用,CoordinatorLayout可以監(jiān)聽其所有子控件的各種事件,然后自動幫我們做出最為合理的響應(yīng)。

2、AppBarLayout:實際上是一個垂直方向的LinearLayout,在內(nèi)部做了很多封裝,并應(yīng)用了一些Material Design的設(shè)計理念。

3、CollapsingToolbarLayout是作用于Toolbar基礎(chǔ)之上的一個布局,CollapsingToolbarLayout可以讓Toolbar的效果變得更加豐富。

4、app:layout_scrollFlags="scroll|exitUntilCollapsed"屬性:srcoll表示CollapsingToolbarLayout會隨著內(nèi)容的滾動一起滾動,exitUntilCollapsed表示當CollapsingToolbarLayout隨著滾動完成折疊之后就保留在界面上,不再移出屏幕。

5、app:contentScrim="?attr/colorPrimary"屬性:用于指定在CollapsingToolbarLayout在趨于折疊狀態(tài)以及折疊之后的背景色。

6、app:layout_collapseMode="pin"屬性:用于指定在控件CollapsingToolbarLayout折疊過程中的折疊模式,pin表示在折疊過程中位置始終不變。

7、app:layout_collapseMode=“parallax” 屬性:表示在折疊的過程中產(chǎn)生一定的錯位偏移。

8、NestedScrollView控件:即有ScrollView控件使用滾動的方式來查看屏幕以外的數(shù)據(jù),NestedScrollView在此基礎(chǔ)之上還增加了嵌套響應(yīng)滾動事件的功能。

9、app:layout_behavior="@string/appbar_scrolling_view_behavior"指定了一個布局行為

10、CardView:用于實現(xiàn)卡片式布局效果的重要控件,額外提供了圓角和陰影的效果。

11、app:cardCornerRadius屬性:指定卡片圓角的弧度。

12、FloatingActionButton懸浮按鈕

關(guān)于控件和屬性就說這么多。

接下來就是實現(xiàn)java代碼了,代碼如下:

public class FruitActivity extends AppCompatActivity {

? ? private CollapsingToolbarLayout collapsing;
? ? private Toolbar toolbar;
? ? private FloatingActionButton floating;
? ? private TextView tv_text;

? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? 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);
? ? ? ? ActionBar actionBar = getSupportActionBar();
? ? ? ? if (actionBar != null) {
? ? ? ? ? ? actionBar.setDisplayHomeAsUpEnabled(true);
? ? ? ? }
? ? ? ? collapsing.setTitle("這是CollapsingToolbarLayout");
? ? ? ? String text = "努力努力再努力";

? ? ? ? tv_text.setText(generateText(text));
? ? ? ? floating.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? Toast.makeText(FruitActivity.this, "您點擊了懸浮按鈕哦!", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? }
? ? ? ? });

? ? }

? ? private String generateText(String text) {
? ? ? ? StringBuilder stringBuilder = new StringBuilder("");
? ? ? ? for (int i = 0; i < 500; i++) {
? ? ? ? ? ? stringBuilder.append(text);
? ? ? ? }
? ? ? ? return stringBuilder.toString();
? ? }

? ? @Override
? ? public boolean onOptionsItemSelected(@NonNull MenuItem item) {
? ? ? ? switch (item.getItemId()) {
? ? ? ? ?? ?//Toolbar左上角默認有一個返回的箭頭,含義是返回上一個活動
? ? ? ? ?? ?//這個按鈕叫做HomeAsUp按鈕,這個按鈕的id永遠都是android.R.id.home
? ? ? ? ? ? case android.R.id.home:
? ? ? ? ? ? ? ? finish();
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? return true;
? ? }
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android 媒體庫數(shù)據(jù)更新方法總結(jié)

    Android 媒體庫數(shù)據(jù)更新方法總結(jié)

    這篇文章主要介紹了Android 媒體庫數(shù)據(jù)更新方法總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • 創(chuàng)建Android守護進程實例(底層服務(wù))

    創(chuàng)建Android守護進程實例(底層服務(wù))

    這篇文章主要介紹了創(chuàng)建Android守護進程實例(底層服務(wù)),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Android中Notification的用法匯總

    Android中Notification的用法匯總

    這篇文章主要介紹了Android中Notification的用法匯總的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • 去除arraylist容器中的相同的對象元素的方法

    去除arraylist容器中的相同的對象元素的方法

    下面小編就為大家?guī)硪黄コ齛rraylist容器中的相同的對象元素的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-09-09
  • Android App打包加固后的APK無法安裝問題解決

    Android App打包加固后的APK無法安裝問題解決

    Android應(yīng)用當中,很多隱私信息都是以 字符串的形式存在的,所以需要加密,本文主要介紹了Android App打包加固后的APK無法安裝問題解決,感興趣的可以了解一下
    2024-01-01
  • Android實現(xiàn)手機多點觸摸畫圓

    Android實現(xiàn)手機多點觸摸畫圓

    這篇文章主要為大家詳細介紹了Android實現(xiàn)手機多點觸摸畫圓,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Android整理好的圖片壓縮工具類

    Android整理好的圖片壓縮工具類

    今天小編就為大家分享一篇關(guān)于Android整理好的圖片壓縮工具類,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Android繪圖技巧使用詳解

    Android繪圖技巧使用詳解

    這篇文章主要為大家詳細介紹了Android繪圖技巧的使用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • 最常用和最難用的Android控件ListView

    最常用和最難用的Android控件ListView

    這篇文章主要為大家詳細介紹了最常用和最難用的Android控件ListView的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Android模擬實現(xiàn)華為系統(tǒng)升級進度條

    Android模擬實現(xiàn)華為系統(tǒng)升級進度條

    這篇文章主要介紹了如何通過Android模擬實現(xiàn)華為在系統(tǒng)升級時顯示的進度條。文中的實現(xiàn)過程講解詳細,感興趣的小伙伴可以動手試一試
    2022-01-01

最新評論