【问题标题】:Multiple elements share a common background多个元素共享一个共同的背景
【发布时间】:2015-12-20 22:17:13
【问题描述】:

我正在制作这个应用程序,我的状态栏、操作栏和页面滑块都紧挨着。我想在它们之上放置一个共同的背景,并在用户滚动时将其淡化。

这很像“Now for Reddit”应用程序的外观。请指导我如何做到这一点。谢谢!

当前代码:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:id="@+id/toolbar"
        app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        >
    </android.support.v7.widget.Toolbar>

    <com.astuetz.PagerSlidingTabStrip
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="48dip"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:background="#3F51B5"
        android:textSize="14sp"
        android:fontFamily="sans-serif-light"
    />

    <android.support.v4.view.ViewPager 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" 
        android:id="@+id/pager"
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        tools:context="com.politicsappv2.MainActivity" />

    <FrameLayout 
        android:id="@+id/container" 
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

对于 PagerSlidingTabStrip,我使用的是this git repo

这是目前的样子:

但这是目标:

【问题讨论】:

    标签: java android animation android-actionbar statusbar


    【解决方案1】:

    我认为做类似事情的一种方法是使用CollapsingToolbarLayoutAppBarLayout。 您可以在其中定义一个 ImageView 作为您的“背景”,它会在滚动时消失。

    一个例子taken from this git repo from TheLittleNaruto

    这是一个 XML 布局示例,取自 this repository

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/detail_backdrop_height"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        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"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp">
    
            <ImageView
                android:id="@+id/backdrop"
                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"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin" />
    
        </android.support.design.widget.CollapsingToolbarLayout>
    
    </android.support.design.widget.AppBarLayout>
    

    编辑:您可以将任何View 作为“标题”放在CollapsingToolbarLayout 上,即使是布局内部的复杂标题,而不仅仅是ImageView

    例子:

    <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"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp">
    
        <include layout="@layout/custom_header" />
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/tool_bar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:elevation="@dimen/mds_elevation_appbar"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:layout_collapseMode="pin" />
    
    </android.support.design.widget.CollapsingToolbarLayout>
    

    custom_header.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/rl_mod_detail_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_collapseMode="parallax"
        android:background="@color/light_gray">
    
        <!-- Any other views in here, in any way you'd like to show them. -->
    
    </RelativeLayout>
    

    【讨论】:

    • 这看起来是一个很好的开始,谢谢!您是否偶然知道我们如何在页面滑块中也使用相同的背景代码? @Mauker
    • 很高兴为您提供帮助! :D 我稍后再举一个例子。
    猜你喜欢
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    相关资源
    最近更新 更多