android collapsible title block

1, First, let's see the effect

2, Code

1,MainActivity.java

package com.example.lcf.myapplication;

import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.coll);
        //Set display text
        collapsingToolbarLayout.setTitle("Study");
        //Set the color of display text
        collapsingToolbarLayout.setExpandedTitleColor(getResources().getColor(R.color.colorAccent));
    }
}

2,activity_main.xml

<?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:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/coll"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="@color/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/bg"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                app:srcCompat="@drawable/ic_launcher_background" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                app:layout_collapseMode="pin">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="10dp"
                    app:srcCompat="@drawable/ic_arrow_back_black_24dp" />
            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        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="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="hello" />
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        app:layout_anchor="@id/appbar"
        app:layout_anchorGravity="bottom|end"
        app:srcCompat="@drawable/ic_edit_black_24dp" />
</android.support.design.widget.CoordinatorLayout>

3, Detailed explanation

  • CollapsingToolbarLayout can only be used as a child layout of AppBarLayout
  • app:contentScrim = "@ color/colorPrimary" folded background color
  • app:layout_scrollFlags = "scroll|exitUntilCollapsed" scroll and keep the toolbar on the interface. If you want to roll out all, just set scroll.
  • The setting of app: layout  collapsemode = "parallax" in ImageView means that there will be an offset during the folding process. In the Toolbar, app: layout  collapsemode = "pin" is set, and the position will not change during the folding process.
  • In order to integrate ImageView with the status bar, you need to set the android:fitsSystemWindows= "true" property, but it is useless to only set the android:fitsSystemWindows= "true" property in ImageView. All its parent layouts, such as CollapsingToolbarLayout, AppBarLayout and CoordinatorLayout, should be set, and the android:statusBarColor property, s should be set in styles.xml Types.xml is as follows:
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimary</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>

</resources>
  • NestedScrollView to implement internal text scrolling
  • FloatingActionButton suspension button, app: layout \ anchor = "@ id/appbar" specifies an anchor point, app: layout \ anchorgravity = "bottom|end" makes the suspension button display in the lower right corner of AppBarLayout.

Tags: Android xml Java encoding

Posted on Sat, 02 May 2020 03:35:56 -0700 by weazy