如何设置类似 Theme.AppCompat.Light.DarkActionBar 的 appcompat-v7 工具栏样式? [英] How do I style appcompat-v7 Toolbar like Theme.AppCompat.Light.DarkActionBar?
问题描述
我正在尝试使用新的支持库工具栏重新创建 Theme.AppCompat.Light.DarkActionBar
的外观.
I'm trying to recreate the look of Theme.AppCompat.Light.DarkActionBar
with the new support library Toolbar.
如果我选择Theme.AppCompat.Light
,我的工具栏会变亮,如果我选择Theme.AppCompat
,它会变暗.(从技术上讲,您必须使用 .NoActionBar
版本,但据我所知,唯一的区别是
If I choose Theme.AppCompat.Light
my toolbar will be light and if I choose Theme.AppCompat
it will be dark. (Technically you have to use the .NoActionBar
version but as far as I can tell the only difference is
<style name="Theme.AppCompat.NoActionBar">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
现在没有 Theme.AppCompat.Light.DarkActionBar
但我天真地认为只要自己制作就足够了
Now there's no Theme.AppCompat.Light.DarkActionBar
but naively I thought it'd be good enough to just make my own
<style name="Theme.AppCompat.Light.DarkActionBar.NoActionBar">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
然而,我的工具栏仍然是 Light
主题.我现在已经花了几个小时尝试混合深色(基本)主题和浅色主题的不同组合,但我找不到一种组合,可以让我在除工具栏之外的所有内容上都拥有浅色背景.
However with this my toolbars are still Light
themed. I've spent hours now trying different combinations of mixing the Dark (base) theme and the Light theme but I just can't find a combination that will let me have light backgrounds on everything but the toolbars.
有没有办法通过 import android.support.v7.widget.Toolbar
获得 AppCompat.Light.DarkActionBar
外观?
Is there a way of getting the AppCompat.Light.DarkActionBar
look with import android.support.v7.widget.Toolbar
's?
推荐答案
为 Light.DarkActionBar
克隆设置工具栏样式的推荐方法是使用 Theme.AppCompat.Light.DarkActionbar
作为父/应用主题,并在样式中添加以下属性以隐藏默认的 ActionBar:
The recommended way to style the Toolbar for a Light.DarkActionBar
clone would be to use Theme.AppCompat.Light.DarkActionbar
as parent/app theme and add the following attributes to the style to hide the default ActionBar:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
然后将以下内容用作您的工具栏:
Then use the following as your Toolbar:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
为了进一步修改,您可以创建扩展 ThemeOverlay.AppCompat.Dark.ActionBar
和 ThemeOverlay.AppCompat.Light
的样式来替换 AppBarLayout-> 中的样式.android:theme
和 Toolbar->app:popupTheme
.另请注意,如果您在主样式中设置了 ?attr/colorPrimary
,这将选取您的 ?attr/colorPrimary
,因此您可能会获得不同的背景颜色.
For further modifications, you would create styles extending ThemeOverlay.AppCompat.Dark.ActionBar
and ThemeOverlay.AppCompat.Light
replacing the ones within AppBarLayout->android:theme
and Toolbar->app:popupTheme
. Also note that this will pick up your ?attr/colorPrimary
if you have set it in your main style so you might get a different background color.
您会在当前项目模板中找到一个很好的示例,其中包含 Android Studio (1.4+) 的 Empty Activity
.
You will find a good example of this is in the current project template with an Empty Activity
of Android Studio (1.4+).
这篇关于如何设置类似 Theme.AppCompat.Light.DarkActionBar 的 appcompat-v7 工具栏样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!