具有TabLayout和ViewPager的CollapsingToolBarLayout

编程入门 行业动态 更新时间:2024-10-09 15:16:28
本文介绍了具有TabLayout和ViewPager的CollapsingToolBarLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试使用CordinatorLayout和CollapsingToolbarLayout,TabLayout和ViewPager创建类似于图像中的布局.我的布局无法正确呈现.

Am try to create a layout similar to the one in the image using a CordinatorLayout and CollapsingToolbarLayout, TabLayout and ViewPager. My Layout doesn't render properly.

这是我的布局

<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="schemas.android/apk/res/android" xmlns:app="schemas.android/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <android.support.design.widget.AppBarLayout android:id="@+id/app_bar_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:fitsSystemWindows="true"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/collapsing_toolbar" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_scrollFlags="scroll|exitUntilCollapsed" app:contentScrim="?attr/colorPrimary" app:expandedTitleMarginStart="48dp" app:expandedTitleMarginEnd="64dp" android:fitsSystemWindows="true"> <ImageView android:id="@+id/image" android:src="@drawable/thumb_placeholder" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" android:fitsSystemWindows="true" app:layout_collapseMode="parallax"/> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:layout_collapseMode="pin" /> <android.support.design.widget.TabLayout xmlns:android="schemas.android/apk/res/android" xmlns:app="schemas.android/apk/res-auto" android:id="@+id/movie_tabs" app:tabGravity="fill" app:tabMode="fixed" android:background="@color/colorPrimary" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabTextColor="@color/textColorPrimary" app:tabSelectedTextColor="@color/textColorPrimary" app:tabIndicatorColor="@color/textColorPrimary" app:tabIndicatorHeight="4dp" app:paddingStart="20dp" app:paddingEnd="20dp"> </android.support.design.widget.TabLayout> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/movieviewpager" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.ViewPager> </android.support.design.widget.CoordinatorLayout>

在我的设备上对其进行测试,我得到了

Testing it on my device i get this

如果有内容,则ViewPager片段会覆盖TabLayout,如果没有内容,则将其显示在ImageView的顶部.我在做什么错,我仍然有点像Android初学者.

The ViewPager Fragment overlays the TabLayout if it has content and displays the TabLayout on top of the ImageView when there is no content. What am i doing wrong, i am still somewhat of an Android noob.

推荐答案

  • CollapsingToolbarLayout 需要一个 minHeight .这告诉父 AppBarLayout 它可以折叠多少.我假设您希望 Toolbar 和 TabLayout 停留在屏幕顶部(因为您使用的是 exitUntilCollapsed 滚动标记).我将使它们都具有 android:layout_height =?attr/actionBarSize" ,并使用该值的两倍作为 CollapsingToolbarLayout 的 minHeight .

  • CollapsingToolbarLayout needs a minHeight. This tells the parent AppBarLayout how much it can collapse. I assume you want the Toolbar and the TabLayout to stay at the top of the screen (because you are using the exitUntilCollapsed scroll flag). I would make both of those have android:layout_height="?attr/actionBarSize" and use double that value as the minHeight of the CollapsingToolbarLayout.

    ViewPager 需要 app:layout_behavior ="@ string/appbar_scrolling_view_behavior" .

  • 更多推荐

    具有TabLayout和ViewPager的CollapsingToolBarLayout

    本文发布于:2023-11-25 20:57:22,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1631314.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:TabLayout   ViewPager   CollapsingToolBarLayout

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!