Android中的视差滚动/可折叠工具栏 [英] Parallax Scrolling/Collapsable Toolbar in Android
问题描述
因此,这是来自udacity项目的Material Design的一些代码.它应该显示一个折叠的工具栏,该工具栏可以从带有文本的照片转换为简单文本.大胆的视频使我相信代码可以工作,但是当我将应用程序下载到手机上时,工具栏没有折叠(操作系统为android 6.0.1,v23).有什么我可以更改以确保工具栏可以折叠的东西吗?
So here is some code from a udacity project for Material Design. It is supposed to show a collapsing toolbar that transforms from a photo with text to simple text. The udacity videos lead me to believe the code worked but when I downloaded the app to my phone, the toolbar did not collapse (the OS is android 6.0.1, v23). Is there something I could change to ensure the toolbar collapses?
代码
xml
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="192dp"
android:background="?colorPrimary">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:collapsedTitleTextAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"
app:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"
app:expandedTitleMarginStart="72dp"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_collapseMode="parallax"
android:scaleType="centerCrop"
android:src="@drawable/eclairs" />
<android.support.v7.widget.Toolbar
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:navigationIcon="@drawable/arrow_left"
app:contentInsetStart="72dp"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<ScrollView
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_height="wrap_content">
<TextView
android:layout_marginTop="16dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Body1"
android:text="@string/cupcake_ipsum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp" />
</ScrollView>
JAVA
package com.example.android.dynamicsurfacesdemo;
import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout)).setTitle("Eclairs");
}
}
推荐答案
我假设您的布局的父级是CoordinatorLayout.
I assume the parent of your layout is a CoordinatorLayout.
要使CoordiantorLayout正常工作,请使用NestedScrollView而不是简单的ScrollView.
To make the CoordiantorLayout work correct use a NestedScrollView instead of a simple ScrollView.
这篇关于Android中的视差滚动/可折叠工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!