我試圖從一個移動TextView到Toolbar滾動時它折疊的時候。
假設我有一個工具列,在它的正下方我有一個視圖,當我向上滾動并且它開始不可見時,我想用此文本更新工具列,這是一個示例。
第一個場景

第二個場景

我一直在閱讀,CollapsingToolbar但是當視圖不那么可見時我可以弄清楚,如果我必須像自定義工具列一樣創建一個在我可以設定的右側的文本,因為它不是標題,它是一個額外的標題的價值。
uj5u.com熱心網友回復:
我可以設定的地方,因為它不是標題,而是標題的額外值。
您可以將標題用于自定義Toolbar為supportActionBar;但是將滾動標志適當地調整到工具列和主布局的布局行為。
如果我必須像自定義工具列一樣在右側創建一個文本
將標題設定為右側;有 2 個屬性,一個用于折疊文本,另一個用于展開,您需要將它們對齊到末尾/右側:
app:collapsedTitleGravity="end"
app:expandedTitleGravity="end"
這是一個演示:
<?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>
確保使用 NoActionBar 主題,并將自定義工具列設定為supportActionBar:
val toolbar = findViewById<Toolbar>(R.id.toolbar)
setSupportActionBar(toolbar)

更新:
perhaps I missexplained the issue, the thing is, I have a Toolbar and just below, I have like a LinearLayout with Text, and when I scroll and this LinearLayout is going to hide I want to show the text it was in the LinearLayout. So the idea is, Something like you did, but instead of having this "Hi" in the Toolbar, have it in a view just below the Toolbar and when I scroll and it starts to get invisible / non readable just put this "Hi" text in the Toolbar.
You can do this with a customized TextView & the title of the CollapsingToolBar.. and programmatically toggle the visibility on both with the OnOffsetChangedListener
First, create the below styles fol collapsed & expanded states:
<style name="Title.Collapsed" parent="android:TextAppearance">
<item name="android:textColor">@android:color/white</item>
<item name="android:textSize">18sp</item>
</style>
<style name="Title.Expanded" parent="android:TextAppearance">
<item name="android:textColor">@android:color/white</item>
<item name="android:textSize">28sp</item>
</style>
These styles will be applied to app:collapsedTitleTextAppearance & app:expandedTitleTextAppearance on the CollapsingToolbarLayout respectively.
layout:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@ id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:fitsSystemWindows="true"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@ id/collapsingToolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:collapsedTitleGravity="end"
app:collapsedTitleTextAppearance="@style/Title.Collapsed"
app:expandedTitleGravity="end"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:expandedTitleTextAppearance="@style/Title.Expanded"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="40dp"
android:background="@android:color/transparent"
android:gravity="end"
android:orientation="vertical"
android:padding="10dp"
app:layout_collapseMode="parallax">
<TextView
android:id="@ id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hi"
android:textSize="28sp" />
</LinearLayout>
<androidx.appcompat.widget.Toolbar
android:id="@ id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/design_default_color_primary"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<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>
檢測 appBarLayout 滾動變化addOnOffsetChangedListener:
val collapsingToolbar = findViewById<CollapsingToolbarLayout>(R.id.collapsingToolbar)
collapsingToolbar.title = ""
title = ""
val appBarLayout = findViewById<AppBarLayout>(R.id.appBarLayout)
appBarLayout.addOnOffsetChangedListener(object : OnOffsetChangedListener {
var isShow = false
var scrollRange = -1
override fun onOffsetChanged(appBarLayout: AppBarLayout, verticalOffset: Int) {
if (scrollRange == -1) {
scrollRange = appBarLayout.totalScrollRange
}
if (scrollRange verticalOffset == 0) {
//when collapsingToolbar at that time display actionbar title
collapsingToolbar.title = "Hi"
isShow = true
} else if (isShow) {
collapsingToolbar.title = ""
isShow = false
}
}
})

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/383363.html
