Android 如何在我的 ActionBar (ActionBarSherlock) 上添加阴影?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11448679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I have a drop shadow on my ActionBar (ActionBarSherlock)?
提问by Debopam Mitra
I am including my styled xml layout:
我包括我的样式 xml 布局:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.Styled" parent="Theme.Sherlock">
<item name="actionBarStyle">@style/Widget.MyApp.ActionBar</item>
<item name="android:actionBarStyle">@style/Widget.MyApp.ActionBar</item>
</style>
<style name="Widget.MyApp.ActionBar" parent="Widget.Sherlock.Light.ActionBar">
<item name="titleTextStyle">@style/Widget.MyApp.TitleTextStyle</item>
<item name="background">@color/red</item>
<item name="android:background">@color/red</item>
<item name="windowContentOverlay">@null</item>
<item name="android:windowContentOverlay">@null</item>
</style>
<style name="Widget.MyApp.TitleTextStyle" parent="TextAppearance.Sherlock.Widget.ActionBar.Title">
<item name="android:textColor">@color/white</item>
<item name="android:textSize">21sp</item>
</style>
</resources>
Some of the search over internet suggests that use windowContentOverlay set to @null. But when i use it in the style xml it doesn't change anything. Can any one help what to do?
互联网上的一些搜索建议使用设置为 @null 的 windowContentOverlay。但是当我在样式 xml 中使用它时,它不会改变任何东西。任何人都可以帮助做什么?
回答by Tomik
If you want to create a shadow below the ActionBar you have to set android:windowContentOverlay
parameter on the application theme (in your code you are incorrectly setting it on the ActionBar style).
如果您想在 ActionBar 下方创建阴影,您必须android:windowContentOverlay
在应用程序主题上设置参数(在您的代码中,您在 ActionBar 样式上错误地设置了它)。
In your example it would be:
在您的示例中,它将是:
<style name="Theme.Styled" parent="Theme.Sherlock">
...
<item name="android:windowContentOverlay">@drawable/my_actionbar_shadow</item>
</style>
Using @null
value removes the shadow.
使用@null
value 可以消除阴影。
This one line sets the shadow on ActionBar on Android 3.0 and newer. However if you are using ActionBarSherlock, it will not work as you expect. It would create the shadow on top of the window over the ActionBarSherlock on Android devices running system older than Android 4.0 (although ActionBar is present in the api since Android 3.0, ActionBarSherlock uses custom implementation for all Android versions older than Android 4.0).
这一行在 Android 3.0 及更新版本的 ActionBar 上设置阴影。但是,如果您使用的是 ActionBarSherlock,它不会像您预期的那样工作。它会在运行早于 Android 4.0 的系统的 Android 设备上的 ActionBarSherlock 上的窗口顶部创建阴影(尽管自 Android 3.0 以来,API 中存在 ActionBar,但 ActionBarSherlock 对所有早于 Android 4.0 的 Android 版本使用自定义实现)。
To create the shadow below ActionBarSherlock you have to set windowContentOverlay
parameter on the application theme (notice the missing android:
).
要在 ActionBarSherlock 下方创建阴影,您必须windowContentOverlay
在应用程序主题上设置参数(注意缺少的android:
)。
<style name="Theme.Styled" parent="Theme.Sherlock">
...
<item name="windowContentOverlay">@drawable/my_actionbar_shadow</item>
</style>
Again, using @null
removes the shadow.
再次,使用@null
删除阴影。
Although this line works for ActionBarSherlock, it doesn't work on android devices running Android 4.0 and newer, no shadow is created under the ActionBar on such devices. So how to combine these two parameters to get the desired shadow under both ActionBar and ActionBarSherlock?
尽管此行适用于 ActionBarSherlock,但它不适用于运行 Android 4.0 及更高版本的 Android 设备,在此类设备上的 ActionBar 下不会创建阴影。那么如何结合这两个参数,在ActionBar和ActionBarSherlock下都得到想要的阴影呢?
Use resource configuration qualifiers, in your case use platform version qualifiers.
In res/values/styles.xml
use the second xml code. And in res/values-v14/styles.xml
use the first xml code. Therefore the ActionBarSherlock version is used by default (for versions pre Android 4.0) and ActionBar version is used for Android 4.0 and newer.
使用资源配置限定符,在您的情况下使用平台版本限定符。在res/values/styles.xml
使用第二个 xml 代码。并在res/values-v14/styles.xml
使用第一个xml代码。因此默认使用 ActionBarSherlock 版本(适用于 Android 4.0 之前的版本),而 ActionBar 版本用于 Android 4.0 及更新版本。
Edit:There is a bugin Android 4.3 (API level 18), android:windowContentOverlay
does not work. It should be fixed in future release. In case you need it fixed in Android 4.3, you can find workarounds linked in the bug report.
编辑:Android 4.3(API 级别 18)中
存在一个错误,android:windowContentOverlay
不起作用。它应该在未来的版本中修复。如果您需要在 Android 4.3 中修复它,您可以在错误报告中找到链接的解决方法。
回答by Stefan
As a previous answer did say use "windowContentOverlay" in the application theme and NOT the action bar style.
正如之前的答案所说,在应用程序主题中使用“windowContentOverlay”而不是操作栏样式。
<style name="Theme.Styled" parent="Theme.Sherlock">
...
<item name="windowContentOverlay">@drawable/my_actionbar_shadow</item>
</style>
If you want a realistic shadow you can find one in the "Your Android Folder"/platforms/android-16/data/res/drawable-hdpi/
如果你想要一个真实的阴影,你可以在“你的 Android 文件夹”/platforms/android-16/data/res/drawable-hdpi/ 中找到一个
ab_solid_shadow_holo.9.png and copy it to your drawable-hdpi folder then the end result is
ab_solid_shadow_holo.9.png 并将其复制到 drawable-hdpi 文件夹中,然后最终结果是
<style name="Theme.Styled" parent="Theme.Sherlock">
...
<item name="windowContentOverlay">@drawable/ab_solid_shadow_holo</item>
</style>
回答by Kevin Liu
In addition, above API21(Lollipop), you will need this in code, too.
另外,在 API21(Lollipop) 之上,你也需要在代码中使用它。
getSupportActionBar().setElevation(0);