如何解决在 CoordinatorLayout 中,如何使 AppBarLayout 占屏幕的 25%?
我有一个 AppBarLayout
,我想占据屏幕的 25%。如果我在没有 Coordinator layout
和 ConstraintLayout
的情况下执行此操作,我可以简单地使用具有 25% 的 Guideline
,但是父布局是一个协调器,所以我不确定在这里做什么。我想在 DP 中不设置高度(例如 150dp
)并在纯 XML 中执行此操作。
<?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/fragprofile_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_gradient"
android:orientation="vertical">
<TextView
android:id="@+id/tv_app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="3dp"
android:fontFamily="@font/concert_one"
android:gravity="center"
android:layout_gravity="center"
android:textColor="@color/colorWhite"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/tablayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_scrollFlags="scroll|enteralways"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:tabIndicatorColor="@color/colorWhite"
app:tabIndicatorHeight="3dp"
app:tabSelectedTextColor="@color/colorWhite"
app:tabTextColor="@color/colorWhite60PercentOpacity" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
解决方法
在不破坏 AppBarLayout
的某些功能的情况下,我认为在 XML 中没有任何可能的方法
但是您可以通过编程实现所需的结果
val appbar: AppBarLayout = findViewById(R.id.fragprofile_app_bar)
val lp = appbar.layoutParams as CoordinatorLayout.LayoutParams
lp.height = (resources.displayMetrics.heightPixels * 0.25).toInt()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。