CollapsingToolbarLayout
很容易。如果您使用Android Studio,则很容易添加。com.project
),选择New->Activity->ScrollingActivity,并将其添加到您的项目中。现在,您只需要对代码进行一些自定义即可。styles.xml
文件并添加这2个样式: <style name="TextAppearance.MyApp.Title.Collapsed" parent="android:TextAppearance">
<item name="android:textColor">@android:color/white</item>
<item name="android:textSize">11sp</item>
</style>
<style name="TextAppearance.MyApp.Title.Expanded" parent="android:TextAppearance">
<item name="android:textColor">@android:color/white</item>
<item name="android:textSize">14sp</item>
</style>
前往您的activity_scrolling.xml文件并在CollapsingToolbarLayout
中设置这两个样式。另外,根据您的需求在CollapsingToolbarLayout
中添加TextView
。就像这样:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.mailcollection.ScrollingActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsingToolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:collapsedTitleTextAppearance="@style/TextAppearance.MyApp.Title.Collapsed"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleTextAppearance="@style/TextAppearance.MyApp.Title.Expanded"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left|bottom"
android:orientation="vertical"
android:padding="10dp"
android:layout_marginBottom="20dp"
app:layout_collapseMode="parallax">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="@string/app_name"
android:textSize="15sp"/>
<TextView
android:id="@+id/tv_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textSize="20sp"
android:text="@string/title_description"/>
</LinearLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_scrolling"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
app:layout_anchor="@id/appBarLayout"
app:layout_anchorGravity="bottom|end"
app:srcCompat="@android:drawable/ic_dialog_email"/>
</android.support.design.widget.CoordinatorLayout>
当您对CollapsingToolbarLayout
进行一些自定义时,您需要在活动文件中实现一些自定义代码。
进入您的ScrollingActivity.java文件:-
public class ScrollingActivity extends AppCompatActivity {
CollapsingToolbarLayout collapsingToolbar;
AppBarLayout appBarLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsingToolbar);
collapsingToolbar.setCollapsedTitleTextAppearance(R.style.TextAppearance_MyApp_Title_Collapsed);
collapsingToolbar.setExpandedTitleTextAppearance(R.style.TextAppearance_MyApp_Title_Expanded);
// This is the most important when you are putting custom TextView in CollapsingToolbar
collapsingToolbar.setTitle(" ");
appBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
boolean isShow = false;
int scrollRange = -1;
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (scrollRange == -1) {
scrollRange = appBarLayout.getTotalScrollRange();
}
if (scrollRange + verticalOffset == 0) {
//when collapsingToolbar at that time display actionbar title
collapsingToolbar.setTitle(getResources().getString(R.string.app_name));
isShow = true;
} else if (isShow) {
//carefull there must a space between double quote otherwise it dose't work
collapsingToolbar.setTitle(" ");
isShow = false;
}
}
});
}
}
CollapsingToolbarLayout
中添加多行TextView
而不是标题,则需要使用此代码。希望您能找到解决方案。对于多行文本,使用以下代码来设置工具栏:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/color"
app:popupTheme="@style/AppTheme.PopupOverlay">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#FFF"
android:inputType="textMultiLine"
/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>