首先,
activity_main.xml布局文件示意图:
其中AppBarLayout下的textView部分也可以为一个imageView。
xml全部代码:
<?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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="256sp">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsingBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="@color/toolbar_bg"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@mipmap/toolbar_bg"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_collapsing"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="卡片式布局"
android:textSize="20sp"
android:layout_margin="20dp"/>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="卡片式布局"
android:textSize="20sp"
android:layout_margin="20dp"/>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="卡片式布局"
android:textSize="20sp"
android:layout_margin="20dp"/>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="卡片式布局"
android:textSize="20sp"
android:layout_margin="20dp"/>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="卡片式布局"
android:textSize="20sp"
android:layout_margin="20dp"/>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="卡片式布局"
android:textSize="20sp"
android:layout_margin="20dp"/>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="卡片式布局"
android:textSize="20sp"
android:layout_margin="20dp"/>
</androidx.cardview.widget.CardView>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
对应的activity:
package com.example.collapsingtoolbarlayout;
import android.os.Bundle;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
private AppBarLayout appBarLayout;
private CollapsingToolbarLayout collapsingToolbarLayout;
private ImageView imageView;
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
toolbar = findViewById(R.id.toolbar_collapsing);
setSupportActionBar(toolbar);
collapsingToolbarLayout = (CollapsingToolbarLayout)findViewById(R.id.collapsingBar);
appBarLayout = (AppBarLayout)findViewById(R.id.appBar);
imageView = (ImageView)findViewById(R.id.imageView);
setTitleToCollapsingToolBarLayout();
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//改变标题栏标题
private void setTitleToCollapsingToolBarLayout(){
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
if (i <= imageView.getHeight() * 4 / 5 ){
collapsingToolbarLayout.setTitle("change title");
collapsingToolbarLayout.setExpandedTitleColor(getResources().getColor(android.R.color.transparent));
collapsingToolbarLayout.setCollapsedTitleTextColor(getResources().getColor(R.color.toolbar_title));
}else {
collapsingToolbarLayout.setTitle("");
}
}
});
}
}
参考文章:
https://www.cnblogs.com/whycxb/p/9374690.html