gpt4 book ai didi

android - CollapsingToolbarLayout 和 TabLayout 导致标题重叠

转载 作者:行者123 更新时间:2023-11-29 14:46:33 24 4
gpt4 key购买 nike

我正在使用 CollapsingToolbarLayout,我试图使 Activity 的标题很好地落入其工具栏,同时不与 TabLayout 重叠。

我已经搜索了几个小时,大多数答案都建议工具栏的自定义高度,但这会导致标题进入工具栏的下部 (android:gravity="bottom")。尝试改变它的重力但没有成功。

有什么办法吗?

这就是我现在得到的: 1

我的 XML 布局如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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"
android:fitsSystemWindows="@bool/isFitSystemWindows">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/awesomeviewpager_appbar"
android:fitsSystemWindows="@bool/isFitSystemWindows"
app:layout_behavior="com.iac.awesomeviewpager.FlingBehavior"
app:theme="@style/ThemeOverlay.AppCompat.Light" >

<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/header_height"
android:fitsSystemWindows="@bool/isFitSystemWindows"
app:expandedTitleGravity="center"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">

<com.flaviofaria.kenburnsview.KenBurnsView
android:id="@+id/cover"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/raton"
android:fitsSystemWindows="@bool/isFitSystemWindows"
android:scaleType="centerCrop"
android:src="@drawable/raton"
app:layout_collapseMode="parallax" />

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:minHeight="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:tabMode="scrollable"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:tabBackground="@android:color/transparent"
app:tabIndicatorColor="@android:color/white"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

谢谢!

最佳答案

我最近发现自己和你有类似的情况。不幸的是,我找不到一个干净利落的解决方案。然而,我确实找到了一个肮脏的 XML 混搭解决方案。

一个关键时刻是意识到 CollapsingToolbarLayout 将从第一个 Toolbarapp:toolbarId< 定义的继承它的终端折叠高度 属性。

一旦克服了这一障碍,如何利用边距和填充来对齐最终结果就只是时间和耐心的问题了。

以下解决方案的关键组件是:

  • @dimen/height 维度
  • @dimen/height_double 维度
  • app:collapsedTitleGravity="top" 属性

@dimen/height@dimen/height_double 的实际值并不像它们之间的关系那么有趣;一个是另一个的两倍大。我想我在测试中分别使用了 56dp112dp

我还在 ToolbarTabLayout 中添加了半透明背景颜色属性,这将突出显示(在运行时)到底发生了什么。

希望这对您有所帮助。


my_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<!--
This one is interesting, especially the
"collapsedTitleGravity" and "expandedTitleMarginBottom"
attributes
-->
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:collapsedTitleGravity="top"
app:expandedTitleMarginBottom="@dimen/height"
app:layout_scrollFlags="scroll|exitUntilCollapsed">

<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="@drawable/pancake"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7" />

<!--
This one is extra interesting, given the relation
between its own height and the TabLayout height.
The layout margins and paddings are dependant on
text size etc.
-->
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="@dimen/height_double"
android:layout_gravity="top"
android:layout_marginBottom="14dp"
android:layout_marginTop="-14dp"
android:background="#33ff0000"
android:paddingTop="12dp"
app:layout_collapseMode="pin" />

<!--
And this one too, given how its "layout_height"
attribute relates to the Toolbar.
-->
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="@dimen/height"
android:layout_gravity="bottom"
android:background="#3300ff00"
app:tabGravity="fill"
app:tabMode="fixed" />

</android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

关于android - CollapsingToolbarLayout 和 TabLayout 导致标题重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35249197/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com