首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用新材料设计在AppBarLayout上覆盖内容

使用新材料设计在AppBarLayout上覆盖内容
EN

Stack Overflow用户
提问于 2015-06-25 00:23:18
回答 4查看 20.5K关注 0票数 29

我想要实现这样的事情。(不是FAB或Snackbar)。如何创建覆盖AppBarLayout的布局?像这样!(例如)

比如Play Store:

我以CoordinatorLayout为内容的AppBarLayout和以RelativeLayout为内容的NestedScrollView如下所示:

代码语言:javascript
复制
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsingToolbarLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:contentScrim="@color/mpc_pink"
        app:expandedTitleMarginStart="@dimen/_40sdp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <de.mypostcardstore.widgets.ItemImageView
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@color/mpc_pink"
            app:layout_collapseMode="parallax"
            app:layout_collapseParallaxMultiplier="0.7" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/article_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:minHeight="?attr/actionBarSize"
            app:contentScrim="@color/mpc_pink"
            app:layout_collapseMode="pin"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />


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

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

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:colorBackground"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <RelativeLayout
        android:layout_width="match_parent".....>

如果有人能帮我,那就太棒了。我在网上找不到任何东西...

提前感谢!

EN

回答 4

Stack Overflow用户

发布于 2015-07-03 17:33:17

只需添加如下内容

代码语言:javascript
复制
app:behavior_overlapTop="64dp"

添加到您的NestedScrollView,它将被放置在展开的工具栏的上方。

此外,您还应该添加如下内容

代码语言:javascript
复制
app:expandedTitleMarginBottom="70dp"

添加到您的CollapsingToolbarLayout,以便标题不会显示在覆盖的滚动内容下。

票数 48
EN

Stack Overflow用户

发布于 2015-06-30 00:57:56

这很简单,真的。您可以通过组合使用ToolBarFrameLayout和内容视图(可以是像第一个示例一样的ListView,或者其他任何内容)来实现这一点。

这个想法是为了让你的FrameLayout拥有和你的ToolBar相同的颜色,给人一种ToolBar比实际大得多的错觉。接下来要做的就是让您的内容视图成为最后一个视图(或者在API21及更高版本中:拥有最高的elevation属性),这样它看起来就像是浮动在前面提到的FrameLayout之上。

请看我的插图:

现在,您已经有了一个重要的想法,下面是一些真正的XML代码片段,用于完成这些工作。(我实际上在我的一个应用程序中使用了这个布局):

代码语言:javascript
复制
<!-- Somewhere in your layout.xml -->

....

    <android.support.v7.widget.Toolbar
        android:id="@+id/tb_toolbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/abc_action_bar_default_height_material"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:contentInsetStart="72dp"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <!-- This is the 'faux' ToolBar I've been telling you about. This is the part that will be overlaid by the content view below. -->
    <FrameLayout
        android:id="@+id/v_toolbar_extension"
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:layout_below="@+id/tb_toolbar"
        android:background="?attr/colorPrimary"
        android:elevation="2dp"/>

    <!-- Normally, I use this FrameLayout as a base for inflating my fragments. You could just use put your content view here. -->
    <FrameLayout
        android:id="@+id/ly_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/tb_toolbar"
        android:elevation="3dp"/>

....

请注意,我的ly_content的高程值比v_toolbar_extension的高。这就是给你想要的‘覆盖工具栏’的效果。

最后但并非最不重要的一点是,您需要在活动的onCreate()中添加下面这一行:

代码语言:javascript
复制
/* Assuming mToolbar exists as a reference to your ToolBar in XML. */
setSupportActionBar(mTbToolbar);
getSupportActionBar().setElevation(0);

这些代码要做的是将你的ToolBar高程设置为零;删除作为ToolBars的默认设置的预设阴影。如果你不这样做,阴影会在你的ToolBarFrameLayout之间形成一道“缝”,从而打破了这两者是相同的错觉。

对于P.S.__,同样重要的是在内容视图的每一面都要有一个填充。这样做,您的内容视图将不会覆盖整个屏幕的宽度(这将使此效果无用)。

注意:我在这里看到了一些很好的答案,他们提到了FrameLayout的缺失,而不是让ToolBar更高。虽然从理论上讲,它可能和我提出的解决方案一样好,但在尝试操作滚动时可能会遇到问题;这样做,您将无法分离ToolBar及其扩展。您将被迫要么将Toolbar设置为静态,要么将所有ToolBar一起滚动(这会让滚动有点奇怪)。

此外,您不能轻松地将自定义可绘制赋值到Toolbar中。因此很难遵循上面给出的Google Play示例。而如果你使用我的解决方案,你所需要做的就是使你的Toolbar透明,并将可绘制的赋值给FrameLayout

票数 15
EN

Stack Overflow用户

发布于 2015-07-02 12:28:48

我有一个类似的需求,我实现了如下所示。

您的活动主题应该扩展Theme.AppCompat.Light.NoActionBar。我创建了布局XML文件,如下所示:

代码语言:javascript
复制
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_main"
    android:layout_width="match_parent"
    android:layout_height="@dimen/action_bar_size_x2"
    android:background="@color/colorPrimary"
    android:minHeight="?attr/actionBarSize" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="@dimen/action_bar_size"
    android:orientation="vertical" >

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:text="@string/app_name"
                android:textSize="24sp" />
        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

活动应该是这样的:

代码语言:javascript
复制
public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar maintoolbar = (Toolbar) findViewById(R.id.toolbar_main);
        setSupportActionBar(maintoolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

我得到的视图是这样的:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31031918

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档