gpt4 book ai didi

android - Material Design - AppCompat 工具栏不显示阴影

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:57:10 26 4
gpt4 key购买 nike

我正在使用 AppCompat V7 的工具栏来替换以前的操作栏,并希望像以前的操作栏一样具有工具栏的阴影。但默认情况下工具栏没有阴影,我已经尝试了 reddit 中提到的修复.但没有运气。

设置阴影的代码:

mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

工具栏布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:minHeight="?attr/actionBarSize"
android:background="#F1F1F1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="0dp"
android:layout_margin="0dp"
foreground="?android:windowContentOverlay">

Activity 布局:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:id="@+id/drawer_layout"
android:layout_height="match_parent">

<!-- activity view -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<FrameLayout android:id="@+id/fragment_container"
android:layout_below="@id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

</RelativeLayout>

<!-- navigation drawer -->
<RelativeLayout
android:id="@+id/left_drawer"
android:layout_gravity="start"
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:divider="#eee"
android:background="#EEE"
android:id="@+id/drawer_header">
<ImageView
android:id="@+id/user_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/user_icon"
android:src="@drawable/ic_action_person"
android:paddingTop="0dp"
android:paddingLeft="0dp"/>
<TextView
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/user_icon"
android:gravity="center"
android:layout_gravity="center_vertical"
android:layout_centerVertical="true"
android:textSize="14sp"
android:text="@string/not_logged_in"
android:paddingTop="0dp"
android:paddingBottom="0dp"/>
</RelativeLayout>
<ListView
android:id="@+id/drawer_list"
android:layout_below="@+id/drawer_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#eee"
android:background="#fff"
android:dividerHeight="0dp" />
</RelativeLayout>

</android.support.v4.widget.DrawerLayout>

在style.xml中设置:

<style name="myAppTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">@color/primaryColor</item>
<item name="colorPrimaryDark">@color/primaryColorDark</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
<item name="android:windowContentOverlay">@drawable/drawer_shadow</item>
</style>

<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">@android:color/black</item>
</style>

有人能帮忙吗?

谢谢!

更新 1:根据 Willis 的建议,我显示了阴影,但它不在工具栏下方,而是在工具栏左侧。 enter image description here

更新 2:我注意到,如果我不在 toolbar.xml 和 styles.xml 中设置 windowContentOverlay,则阴影实际上位于工具栏的顶部。 enter image description here

最佳答案

那两个是完全不同的影子。 verticalDrawerLayout 的。它应该显示在展开的抽屉旁边。 水平windowContentOverlay 的一部分,在 LOLLIPOP 下面的 API 上(在 LOLLIPOP 上是 @null).

当您使用 Toolbar 小部件时,工具栏不再是窗口装饰的一部分,因此阴影从窗口顶部开始覆盖工具栏而不是 低于它(因此您希望 windowContentOverlay@null)。此外,您需要在 LOLLIPOP 之前的工具栏下方添加一个额外的空 View,并将其背景设置为可绘制的垂直阴影(8dp 高渐变来自 #20000000#00000000 效果最好)。在 LOLLIPOP 上,您可以改为在工具栏上设置 8dp 高度。

注意:使用与抽屉阴影相同但水平的渐变以获得最佳效果。

关于android - Material Design - AppCompat 工具栏不显示阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28181092/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com