- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想像这样实现一个CollapsingToolbarLayout
:
这里的挑战是我需要一个带有 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 都在滚动,Toolbar
和 TabLayout
都不会固定到屏幕顶部。
我学习了很多教程和 stackoverflow 的答案,f.e.
- https://antonioleiva.com/collapsing-toolbar-layout/
- http://blog.iamsuleiman.com/parallax-scrolling-tabs-design-support-library/
他们都很好地解释了如何使用 CollapsingToolbarLayout
。我认为我的实现的问题是包含 ImageView
和 Toolbar
的 RelativeLayout
。当删除 RelativeLayout
和 ImageView
并将 Toolbar
的 collapseMode 设置为“固定”时,一切都按预期工作,Toolbar
和 Tablayout
在用户滚动时固定在屏幕顶部。但不幸的是,我需要在 toolbar
上方安装 ImageView
才能加载 GIF 作为 Toolbar
背景。
也许你们中的某个人对如何解决这个问题有了绝妙的想法。或者你有另一个想法如何实现所需的行为?请让我知道:)
更新:我创建了一个示例项目 ( https://drive.google.com/open?id=0B1aHkcAaWIA-dHBTZnUyeUt3eTQ ),您可以使用它重现错误的滚动行为。
最佳答案
#. 这里我用了CollapsingToolbarLayout
高度为 300dp
这是 Toolbar
的总和, ImageSlider
和 TabLayout
高度。二手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
固定在 top
和 visible
滚动之前或之后。
#. 添加了 Toolbar
下面ImageView
显示Toolbar
在 ImageView
.因为我没有设置任何 background
颜色为 Toolbar
, 它将作为 transparent
工作.喜欢ImageView
, 添加 app:layout_collapseMode="pin"
至 Toolbar
至 pin
它总是在 top
.添加属性 android:layout_height="104dp"
, 显示 Tablayout
下面Toolbar
在collapsed
期间状态。这里104dp
是Toolbar
高度( 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>
输出:
仅供引用,因为您正在使用自定义 Toolbar
与 TextView
, 你必须隐藏默认 title
的 ActionBar
.为此,请在您的 Activity
中使用以下代码:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
希望对你有帮助~
关于Android:带有 ImageView 工具栏背景的 CollapsingToolbarLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43674691/
我正在尝试创建一个演示应用程序,我想在其中创建一个 ImageView ,该 ImageView 相互重叠,x 和 y 坐标略有变化(类似于所有纸牌相互重叠的任何纸牌游戏,但我们可以看到数字)。如果用
您好,我有一个要求,我需要通过拖动 ImageView 的右角来调整 ImageView 的大小和旋转 ImageView 。 我可以通过拖动 ImageView 的角成功地调整 ImageView
我目前正在尝试使用 JavaFX 在 ImageView 中居中图像。 所以我在 View 中加载图像: Image img = new Image("..."); imageView.setImag
我有一个非常简单的问题要问:我需要在屏幕右下角的 ImageView 上放一个小 Logo ,整个屏幕都很大,但我不知道如何设置坐标或如何设置说 ImageViews 处于相对位置。 像这样: 最佳答
我想像这样制作一个 ImageView 覆盖另一个 ImageView;只有一半的绿色圆圈覆盖了图像: 我试过使用 RelativeLayout 并将两个 ImageView 都放在里面。然后我使用
我们如何使 ImageView 的宽度和高度等于父 ConstraintLayout 的宽度?所以它显示一个全宽的方形 ImageView。 通常如何设置某个小部件的高度等于其他小部件的宽度? 最佳答
有点像刽子手 如果 ImageView 中有东西,如何跳过 ImageView 并检查其中是否有东西尝试检查 imageView 中是否有图像 代码 final boolean imageT
我正在用 swift 制作一种“保持在线条之间”iPhone 应用程序,我有一个移动 ImageView 和 2 个代表线条的 ImageView 。如何检测移动 ImageView 何时撞击或接触线
我正在做一个应用程序,用户通过单击图像(ImageView1)可以在另一个ImageView2中看到它。所以我尝试在变量中获取 ImageView1 的图像 BufferedImage img= Sw
我有以下看法: 如果我点击任何 ImageButton 以全屏模式显示相应的图像。例如,如果我点击 Black ,它会显示 ImageView ,如: 我还为一些图像按钮将背景设置为全屏颜色:如果我单
我在名为 DialButton2 的类中扩展了 ImageView 类(不要担心类的名称,它无关紧要)。 DialButton2 类所做的只是显示位于可绘制文件夹中的任 Intent 像。 packa
我正在使用 Xcode 8.1 和 swift 3.0 开发应用。 使用 Storyboard ,我扩展了一个 ImageView 以占据整个 View ,并为其设置了一张图片以用作背景图像(使用 s
如何将我的 imageView 裁剪成我在项目中有图像的气泡形状。 最佳答案 以下是调整大小或裁剪图像的简单代码,您只需根据需要为图像传递高度或宽度即可: 获取裁剪图像: UIImage *cropp
我有 92 张图像,我希望有一个指示符(例如复选标记)来指示图像已解锁。我在 .png 文件中有复选标记,我首先尝试的是为每个图像制作单独的副本,并将复选标记放在 Photoshop 中的图像顶部。但
我有两个 ImageView ,它们看起来像这样。我想将 TouchListener 添加到 imageview1。但听众无法正常工作。我正在将 imageview2 添加到 android:foc
我有一个列表布局,其中包含如下所示的项目: 首先有一个 ImageView(灯光),然后是两个 textView。所有这些都在 TableLayout 中。 (来源:http://code.googl
我想知道如何将 ImageView 元素添加到 JavaFx 2.1 中的区域元素。 也许我把这个元素的用法弄错了,但 AFAIK 它也是子元素的容器。 背景是我需要一个定义大小的区域,该区域应显示独
我在调整 StackPane 中的 ImageView 大小时遇到了问题:如果 StackPane 是根容器并且我将图像的高度和宽度绑定(bind)到 StackPane,那么一切都很好。
** 我有两个 ImageView ,一个用于背景,一个用于前景,我正在前景 ImageView 上绘图。对于删除功能,我这样做是因为我不想在删除时删除背景图像。它正在执行很好。现在我想保存该图像(背
我正在尝试为 watch 应用程序创建 ImageView ,我正在尝试以编程方式创建,但经典 View Controller 中使用的代码不起作用。 let imageName = "yourIma
我是一名优秀的程序员,十分优秀!