我使用新的支持库,可以拥有Toolbar实例并将其设置为活动的ActionBar(或使用默认ActionBar)。
如何自定义由Toolbar(或默认ActionBar)产生的阴影?
我尝试使用"setElevation"(在两者上),但似乎在Android Lollipop上没有任何作用。
而且,我找不到如何自定义早期版本中的阴影。也许有API可以实现这一点?或者至少有可绘制的图像(尽管我已经尝试过)?
OK,我已经找到了以下内容:
对于Lollipop和pre-Lollipop,当您的活动使用普通的AppCompat主题(例如"Theme.AppCompat.Light")时,阴影应该看起来很好。
但是,当您使用"setSupportActionBar"(并使用适当的主题,例如"Theme.AppCompat.Light.NoActionBar")时,您将在阴影方面遇到一些问题。
对于pre-Lollipop,您可以在工具栏下方放置一个FrameLayout,它看起来与用于正常主题的FrameLayout完全相同,如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:foreground="?android:windowContentOverlay"
android:visibility="@integer/action_bar_shadow_background_visibility" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:text="@string/hello_world" />
</RelativeLayout>
您可以将FrameLayout用作activity的内容,或使用我做的方法。您还可以通过像我所做的那样将整数0放置在pre-Lollipop上,将2放置在Lollipop及以上版本上来设置其可见性,如“action_bar_shadow_background_visibility”中所示。
这是一个丑陋的解决方案,但它却非常有效,但仅适用于Pre-Lollipop版本。
无论如何,我仍然找不到一种方法来很好地显示Lollipop版本的阴影,以防我使用“setSupportActionBar”。我尝试在工具栏和操作栏上使用“setElevation”(在调用“setSupportActionBar”后立即使用“getSupportActionBar”)。
我也不知道如何获得操作栏的标准高度,因为我注意到很多教程都说要为工具栏使用“minHeight”。对此,我会非常感激帮助。
有谁能帮我解决这个问题吗?
再次强调,这是我的需求:
- 对于Pre-Lollipop,请显示与Toolbar和ActionBar支持库中使用的相同阴影。
- 对于Lollipop,使用Lollipop自带的阴影。
- 额外功能:在Lollipop上自定义阴影使其更加“精致”(并可能在所有版本上进行自定义)。
- 额外功能:为什么工具栏的minHeight获得的是操作栏高度的值,而不是本身的高度?
编辑:好的,这是我迄今为止为了模仿Lollipop阴影所做的工作:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
layout="@layout/toolbar_action_bar_shadow" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:text="@string/hello_world" />
</RelativeLayout>
res/layout/toolbar_action_bar_shadow.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:windowContentOverlay" />
res/layout-v21/toolbar_action_bar_shadow.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/msl__action_bar_shadow" />
res/drawable-v21/res/layout/msl__action_bar_shadow.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- intended for lollipop and above -->
<item>
<shape
android:dither="true"
android:shape="rectangle" >
<gradient
android:angle="270"
android:endColor="#00000000"
android:startColor="#33000000" />
<size android:height="10dp" />
</shape>
</item>
</layer-list>
虽然不完全相同,但是它们很相似。
编辑:我仍然不知道为什么它会发生。我认为我在问题上编写的代码是具有误导性的,因为所有需要做的就是设置一个背景。但是在我测试的应用程序中仍然没有帮助。我尝试修复的应用程序在导航抽屉上方有工具栏。
一度我以为可能是因为我做了一个自定义的工具栏(继承自Toolbar),并且根据导航抽屉改变了其Alpha值,但即使我使用正常的工具栏并禁用与Alpha相关的所有内容,它仍然无法正常工作。此外,不仅如此,在一个全新的项目上,我尝试为工具栏设置半透明的背景,结果根据“setElevation”方法产生了阴影。
现在对我来说更难找到这个问题的原因了,因为似乎不是由于透明度或自定义Toolbar类造成的...
这里是具有工具栏的活动的布局,如果可以的话,请参考一下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:app="http://schemas.android.com/apk/res-auto"
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" >
<android.support.v4.widget.DrawerLayout
android:id="@+id/activity_main__drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/activity_main__fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:layout_marginTop="?attr/actionBarSize"
layout="@layout/activity_main__sliding_menu" />
</android.support.v4.widget.DrawerLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.app.ui.Toolbar
android:id="@+id/activity_main__toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:layoutDirection="ltr"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
tools:ignore="UnusedAttribute" />
<!-- <include -->
<!-- android:layout_width="match_parent" -->
<!-- android:layout_height="wrap_content" -->
<!-- layout="@layout/toolbar_action_bar_shadow" /> -->
</LinearLayout>
</FrameLayout>