您好,我正在开发小型 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/