我有這個場景:
我創造了這個
<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>
然后這是我的布局
<?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>
然后我將折疊影片制作為:
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
}
}
})
它完美無缺,現在的問題是我想添加一個粘性按鈕,但我無法告訴 NestedScrollView 對齊我的按鈕 bottomToTopOf 因為它不在 ConstraintLayout 內,也不是它的子項,所以它不會崩潰編譯但按鈕隱藏了滾動視圖的最后一部分......我試圖添加marginBottom="buttonSize"并且在布局設計中它看起來不錯,但是在編譯時它不會執行折疊影片,因為我已經添加了marginBottom. 我怎樣才能解決這個問題?我需要這個 NestedScrollView 不是高度 match_parent 即使它是 wrap_content 它與父級對齊并且按鈕重疊。
uj5u.com熱心網友回復:
您可以將CoordinatorLayout& 底部包裹Button在 a 中ConstraintLayout作為根布局;現在CoordinatorLayout可以約束按鈕的頂部,而不是具有等于整個螢屏高度的高度。
所以,布局將是:
<ConstraintLayout>
<CoordinatorLayout>
<AppBarLayout>
<CollapsingToolbarLayout>
<NestedScrollView>
</CoordinatorLayout>
<Button/>
</ConstraintLayout>
應用于您的布局:
<androidx.constraintlayout.widget.ConstraintLayout 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">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@ id/button"
app:layout_constraintTop_toTopOf="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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/long_text" />
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<Button
android:id="@ id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:insetLeft="0dp"
android:text="OK"
android:insetTop="0dp"
android:insetRight="0dp"
android:insetBottom="0dp"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/403855.html
標籤:
下一篇:當沒有resource_id時,如何在appium測驗中等待并抓取android.widget.TextView的文本?
