Android widget AppBarLayout 总是排在最前面

标签 android android-toolbar android-relativelayout

您好,我正在开发小型 Android 应用程序,我试图在其中使用 AppBarLayout 显示工具栏。我想在我的工具栏顶部显示一些 View 。我尝试了以下方式:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.nileshkashid.samplesearchbarapplication.Main2Activity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/toolbar_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/transparent"
            />

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="@android:color/holo_red_dark"
        ></RelativeLayout>

    <RelativeLayout
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:background="@android:color/holo_green_dark"
        ></RelativeLayout>


</RelativeLayout>

所以在上面的布局中,稍后两个相关布局都在我的 AppBarLayout 下。但我想在我的 toolbar_view 之上显示它们。我错过了什么或做错了什么。需要一些帮助。谢谢。

最佳答案

我真的对这个解决方案不满意,但到目前为止我发现了这个: 尝试将 android:elevation="5dp" 添加到 AppBarLayout 下方的 RelativeLayout:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:elevation="5dp"
    android:background="@android:color/holo_red_dark" />

我也遇到了这个问题,所以我会尝试对此进行更多调查。 (对我来说魔法值是5dp,不要问为什么。)

或者,您可以将 View 放在 AppBarLayout 中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"    tools:context="com.example.nileshkashid.samplesearchbarapplication.Main2Activity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/toolbar_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!-- You have to use a RelativeLayout here, because AppBarLayout is a LinearLayout. -->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:background="@android:color/holo_red_dark" />

        </RelativeLayout>

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

    ....

</RelativeLayout>

关于Android widget AppBarLayout 总是排在最前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35379214/

相关文章:

java - 在应用程序中的特定页面上绘制背景颜色

java - 我想以 2 秒的间隔从谷歌地图上的 arrayList 添加标记

android-tabs - 使用支持库 v7 :21 with Toolbar in Android Developer Tools for existing project 添加滑动标签

android - 以编程方式创建相对布局

android - ListView 下方的按钮

android - Sencha touch lib 不适用于 Chrome 43

android - 删除后在 Google Play 上更新 Android 应用程序

android - 图像为白色时的 CollapsingToolbarLayout

android - 为什么工具栏在屏幕中间?

android - 如何使用 RelativeLayout 实现以下结果?