[英]How can I update the toolbar when scrolling with CollapsingToolbar
I'm trying to move from one TextView
to the Toolbar
when it collapse when scrolling.当滚动时折叠时,我试图从一个TextView
移动到Toolbar
。
Let's say I have a Toolbar and just below of it I have a View, when I scroll up and it starts to not be visible at all I want to update the toolbar with this text, here's an example.假设我有一个工具栏,就在它的下方我有一个视图,当我向上滚动并且它开始根本不可见时,我想用这个文本更新工具栏,这是一个例子。
I've been reading with the CollapsingToolbar
but I can figure it out when the view is not that visible and if I have to create like a custom Toolbar with a text at the right where I can set, because it's not the title, it's an extra value of the title.我一直在阅读CollapsingToolbar
但我可以弄清楚当视图不那么可见时,如果我必须像自定义工具栏一样在我可以设置的右侧创建一个文本,因为它不是标题,它是标题的额外价值。
where I can set, because it's not the title, it's an extra value of the title.我可以在哪里设置,因为它不是标题,它是标题的额外值。
You can use the title for a customized Toolbar
as supportActionBar
;您可以将自定义Toolbar
的标题用作supportActionBar
; but adjust the scrolling flags appropriately to the toolbar & the layout behaviour to the main layout.但是将滚动标志适当地调整到工具栏和布局行为到主布局。
if I have to create like a custom Toolbar with a text at the right如果我必须创建一个自定义工具栏,右侧有一个文本
To set the title to the right;将标题设置在右侧; there are 2 properties, one for the collapsed text, and the other for the expanded, you need to align them to the end/right:有 2 个属性,一个用于折叠文本,另一个用于展开文本,您需要将它们对齐到末尾/右侧:
app:collapsedTitleGravity="end"
app:expandedTitleGravity="end"
Here is a demo:这是一个演示:
<?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=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbarlayout"
android:layout_width="match_parent"
android:layout_height="100dp"
android:paddingEnd="8dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:collapsedTitleGravity="end"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleGravity="end"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:title="Hi">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:title="Hi"
app:titleTextColor="#fff" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<!-- Main Layout -->
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/appbarlayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/long_text" />
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Make sure to use a NoActionBar theme, and set the custom Toolbar as the supportActionBar
:确保使用 NoActionBar 主题,并将自定义 Toolbar 设置为supportActionBar
:
val toolbar = findViewById<Toolbar>(R.id.toolbar)
setSupportActionBar(toolbar)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.