gpt4 book ai didi

Android:带有 ImageView 工具栏背景的 CollapsingToolbarLayout

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:59:01 25 4
gpt4 key购买 nike

我想像这样实现一个CollapsingToolbarLayout:

enter image description here

这里的挑战是我需要一个带有 gif 背景的自定义 Toolbar,因此我需要一个 ImageView 作为 Toolbar 。您可以在以下 xml 中看到我的实现:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:background="@color/colorAccent"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/htab_collapse_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:contentScrim="@color/colorAccent"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

<!-- Container which should be scrolled parallax and contains the image gallery -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax">

<RelativeLayout
android:id="@+id/image_layer"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<android.support.v4.view.ViewPager
android:id="@+id/image_gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:layout_marginTop="?attr/actionBarSize"/>

<LinearLayout
android:id="@+id/image_indicators"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="@dimen/defaultPadding"
android:gravity="center"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</LinearLayout>

<!-- Container which contains the background for the toolbar and the toolbar itself -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin">

<ImageView
android:id="@+id/toolbar_background"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:scaleType="centerCrop"
android:layout_gravity="top"
android:background="@color/colorPrimaryLight"/>

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">

<de.views.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:textColor="#FF0000"
android:textStyle="bold"
android:gravity="center"
android:maxLines="1"
android:ellipsize="end"
android:layout_gravity="center"
android:id="@+id/toolbar_title"
tools:text="Restauranttitel"/>
</android.support.v7.widget.Toolbar>
</RelativeLayout>

<!-- Tablayout -->
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"
app:tabBackground="@drawable/selected_tab_background"
app:tabIndicatorColor="@android:color/transparent"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabMaxWidth="2000dp"/> <!-- we need to set this value to a very big value so that a single tab gets displayed over the full width too -->

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

<FrameLayout
android:id="@+id/activity_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

通过此实现,整个 View 都在滚动,ToolbarTabLayout 都不会固定到屏幕顶部。

我学习了很多教程和 stackoverflow 的答案,f.e.

他们都很好地解释了如何使用 CollapsingToolbarLayout。我认为我的实现的问题是包含 ImageViewToolbarRelativeLayout。当删除 RelativeLayoutImageView 并将 Toolbar 的 collapseMode 设置为“固定”时,一切都按预期工作,ToolbarTablayout 在用户滚动时固定在屏幕顶部。但不幸的是,我需要在 toolbar 上方安装 ImageView 才能加载 GIF 作为 Toolbar 背景。

也许你们中的某个人对如何解决这个问题有了绝妙的想法。或者你有另一个想法如何实现所需的行为?请让我知道:)

更新:我创建了一个示例项目 ( https://drive.google.com/open?id=0B1aHkcAaWIA-dHBTZnUyeUt3eTQ ),您可以使用它重现错误的滚动行为。

最佳答案

#. 这里我用了CollapsingToolbarLayout高度为 300dp这是 Toolbar 的总和, ImageSliderTabLayout高度。二手app:titleEnabled="false"隐藏 CollapsingToolbar title .

#. 里面CollapsingToolbarLayout , 添加 RelativeLayout作为 ViewPager 的容器(图片)和 LinearLayout (指标)。添加属性 android:layout_marginTop="?attr/actionBarSize"将其放在 ToolBar 下面和添加 app:layout_collapseMode="parallax"显示parallax滚动期间的效果。

#. 下面RelativeLayout , 添加了一个 ImageView显示GIF上面的图像。添加属性 android:layout_height="?attr/actionBarSize"使其高度为Toolbar高度。添加属性 app:layout_collapseMode="pin"保持ImageView固定在 topvisible滚动之前或之后。

#. 添加了 Toolbar下面ImageView显示ToolbarImageView .因为我没有设置任何 background颜色为 Toolbar , 它将作为 transparent 工作.喜欢ImageView , 添加 app:layout_collapseMode="pin"Toolbarpin它总是在 top .添加属性 android:layout_height="104dp" , 显示 Tablayout下面Toolbarcollapsed期间状态。这里104dpToolbar高度( 56dp )+ Tablayout高度( 48dp )。

#. 最后加了TabLayout下面Toolbar并添加了属性 android:layout_gravity="bottom"显示在 CollapsingToolbarLayout 的底部.

这是简化的工作 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:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/htab_collapse_toolbar"
android:layout_width="match_parent"
android:layout_height="300dp"
android:fitsSystemWindows="true"
app:titleEnabled="false"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

<!-- Image slider container -->
<RelativeLayout
android:id="@+id/image_layer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
app:layout_collapseMode="parallax">

<!-- ViewPager -->
<android.support.v4.view.ViewPager
android:id="@+id/image_gallery"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<!-- Pager Indicator Container -->
<LinearLayout
android:id="@+id/image_indicators"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="56dp"
android:orientation="horizontal"
android:padding="8dp"
android:gravity="center"
android:background="@color/black"/>
</RelativeLayout>

<!-- Toolbar background :: GIF -->
<ImageView
android:id="@+id/toolbar_background"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:scaleType="centerCrop"
app:layout_collapseMode="pin"
android:src="@drawable/dummy"/>

<!-- Toolbar -->
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="104dp"
android:minHeight="?attr/actionBarSize"
app:layout_collapseMode="pin">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:textColor="#000"
android:textStyle="bold"
android:gravity="center"
android:maxLines="1"
android:ellipsize="end"
android:layout_gravity="top"
android:id="@+id/toolbar_title"
android:text="Restaurant Title"/>
</android.support.v7.widget.Toolbar>

<!-- TabLayout -->
<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:tabBackground="@android:color/holo_red_dark"
app:tabIndicatorColor="@android:color/transparent"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabMaxWidth="2000dp"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<!-- Container for TAB'S Fragments -->
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>

输出:

enter image description here

仅供引用,因为您正在使用自定义 ToolbarTextView , 你必须隐藏默认 titleActionBar .为此,请在您的 Activity 中使用以下代码:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");

希望对你有帮助~

关于Android:带有 ImageView 工具栏背景的 CollapsingToolbarLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43674691/

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