Android 我如何像 Theme.AppCompat.Light.DarkActionBar 一样设置 appcompat-v7 工具栏的样式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26503149/
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 do I style appcompat-v7 Toolbar like Theme.AppCompat.Light.DarkActionBar?
提问by Liminal
I'm trying to recreate the look of Theme.AppCompat.Light.DarkActionBar
with the new support library Toolbar.
我正在尝试Theme.AppCompat.Light.DarkActionBar
使用新的支持库工具栏重新创建外观。
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
如果我选择Theme.AppCompat.Light
我的工具栏会很亮,如果我选择Theme.AppCompat
它会很暗。(从技术上讲,您必须使用该.NoActionBar
版本,但据我所知,唯一的区别是
<style name="Theme.AppCompat.NoActionBar">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
Now there's no Theme.AppCompat.Light.DarkActionBar
but naively I thought it'd be good enough to just make my own
现在没有,Theme.AppCompat.Light.DarkActionBar
但我天真地认为只要自己做就足够了
<style name="Theme.AppCompat.Light.DarkActionBar.NoActionBar">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
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.
但是有了这个,我的工具栏仍然是Light
主题。我现在已经花了几个小时尝试混合深色(基本)主题和浅色主题的不同组合,但我只是找不到一种组合,可以让我在除工具栏之外的所有内容上都拥有浅色背景。
Is there a way of getting the AppCompat.Light.DarkActionBar
look with import android.support.v7.widget.Toolbar
's?
有没有办法AppCompat.Light.DarkActionBar
通过 import获得外观android.support.v7.widget.Toolbar
?
回答by oRRs
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:
为Light.DarkActionBar
克隆设置工具栏样式的推荐方法是Theme.AppCompat.Light.DarkActionbar
用作父/应用主题,并将以下属性添加到样式以隐藏默认操作栏:
<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>
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.
对于进一步的修改,你会创建样式扩展ThemeOverlay.AppCompat.Dark.ActionBar
和ThemeOverlay.AppCompat.Light
内更换者AppBarLayout->android:theme
和Toolbar->app:popupTheme
。另请注意,?attr/colorPrimary
如果您已将其设置为主要样式,这将拾取您的样式,因此您可能会获得不同的背景颜色。
You will find a good example of this is in the current project template with an Empty Activity
of Android Studio (1.4+).
您会在当前的项目模板中找到一个很好的例子,其中包含Empty Activity
Android Studio (1.4+)。
回答by LordRaydenMK
Edit: After updating to appcompat-v7:22.1.1 and using AppCompatActivity
instead of ActionBarActivity
my styles.xml looks like:
编辑:更新到 appcompat-v7:22.1.1 并使用AppCompatActivity
而不是ActionBarActivity
我的 styles.xml 看起来像:
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>
Note: This means I am using a Toolbar
provided by the framework (NOT included in an XML file).
注意:这意味着我使用的Toolbar
是框架提供的(不包含在 XML 文件中)。
This worked for me:
styles.xml file:
这对我
有用:styles.xml 文件:
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>
Update: A quote from Gabriele Mariotti's blog.
更新:引自Gabriele Mariotti 的博客。
With the new Toolbar you can apply a style and a theme. They are different! The style is local to the Toolbar view, for example the background color. The app:theme is instead global to all ui elements inflated in the Toolbar, for example the color of the title and icons.
使用新的工具栏,您可以应用样式和主题。它们是不同的!样式是工具栏视图的局部样式,例如背景颜色。app:theme 对工具栏中的所有 ui 元素是全局的,例如标题和图标的颜色。
回答by Liminal
Ok after having sunk way to much time into this problem this is the way I managed to get the appearance I was hoping for. I'm making it a separate answer so I can get everything in one place.
好的,在这个问题上花了很多时间之后,这就是我设法获得我希望的外观的方式。我把它作为一个单独的答案,这样我就可以把所有的东西都放在一个地方。
It's a combination of factors.
这是多种因素的组合。
Firstly, don't try to get the toolbars to play nice through just themes. It seems to be impossible.
首先,不要试图让工具栏仅通过主题就能很好地发挥作用。这似乎是不可能的。
So apply themes explicitly to your Toolbars like in oRRs answer
因此,将主题明确应用于您的工具栏,就像在oRRs 答案中一样
layout/toolbar.xml
布局/工具栏.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:theme="@style/Dark.Overlay"
app:popupTheme="@style/Dark.Overlay.LightPopup" />
However this is the magic sauce. In order to actually get the background colors I was hoping for you have to override the background
attribute in your Toolbar themes
然而,这是神奇的酱汁。为了实际获得背景颜色,我希望您必须覆盖background
工具栏主题中的属性
values/styles.xml:
值/styles.xml:
<!--
I expected android:colorBackground to be what I was looking for but
it seems you have to override android:background
-->
<style name="Dark.Overlay" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:background">?attr/colorPrimary</item>
</style>
<style name="Dark.Overlay.LightPopup" parent="ThemeOverlay.AppCompat.Light">
<item name="android:background">@color/material_grey_200</item>
</style>
then just include your toolbar layout in your other layouts
然后只需在其他布局中包含您的工具栏布局
<include android:id="@+id/mytoolbar" layout="@layout/toolbar" />
and you're good to go.
你可以走了。
Hope this helps someone else so you don't have to spend as much time on this as I have.
希望这对其他人有所帮助,这样您就不必像我一样在此上花费太多时间。
(if anyone can figure out how to make this work using just themes, ie not having to apply the themes explicitly in the layout files I'll gladly support their answer instead)
(如果有人能弄清楚如何仅使用主题来完成这项工作,即不必在布局文件中明确应用主题,我将很乐意支持他们的答案)
EDIT:
编辑:
So apparently posting a more complete answer was a downvote magnet so I'll just accept the imcomplete answer above but leave this answer here in case someone actually needs it. Feel free to keep downvoting if it makes you happy though.
所以显然发布一个更完整的答案是一个downvote 磁铁所以我只会接受上面不完整的答案,但把这个答案留在这里以防万一有人真的需要它。如果它让你开心,请随意继续投票。
回答by Don
The cleanest way I found to do this is create a child of 'ThemeOverlay.AppCompat.Dark.ActionBar'. In the example, I set the Toolbar's background color to RED and text's color to BLUE.
我找到的最干净的方法是创建一个“ ThemeOverlay.AppCompat.Dark.ActionBar”的孩子。在示例中,我将工具栏的背景颜色设置为红色,将文本颜色设置为蓝色。
<style name="MyToolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:background">#FF0000</item>
<item name="android:textColorPrimary">#0000FF</item>
</style>
You can then apply your theme to the toolbar:
然后,您可以将主题应用到工具栏:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:theme="@style/MyToolbar"
android:minHeight="?attr/actionBarSize"/>
回答by Arnav Rao
To customize tool bar style, first create tool bar custom style inheriting Widget.AppCompat.Toolbar, override properties and then add it to custom app theme as shown below, see http://www.zoftino.com/android-toolbar-tutorialfor more information tool bar and styles.
要自定义工具栏样式,首先要创建工具栏自定义样式继承Widget.AppCompat.Toolbar,覆盖的属性,然后,如下所示添加到自定义应用程序的主题,请参阅http://www.zoftino.com/android-toolbar-tutorial为更多信息工具栏和样式。
<style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="toolbarStyle">@style/MyToolBarStyle</item>
</style>
<style name="MyToolBarStyle" parent="Widget.AppCompat.Toolbar">
<item name="android:background">#80deea</item>
<item name="titleTextAppearance">@style/MyTitleTextAppearance</item>
<item name="subtitleTextAppearance">@style/MySubTitleTextAppearance</item>
</style>
<style name="MyTitleTextAppearance" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
<item name="android:textSize">35dp</item>
<item name="android:textColor">#ff3d00</item>
</style>
<style name="MySubTitleTextAppearance" parent="TextAppearance.Widget.AppCompat.Toolbar.Subtitle">
<item name="android:textSize">30dp</item>
<item name="android:textColor">#1976d2</item>
</style>
回答by Yong
Yout can try this below.
你可以在下面试试这个。
<style name="MyToolbar" parent="Widget.AppCompat.Toolbar">
<!-- your code here -->
</style>
And the detail elements you can find them in https://developer.android.com/reference/android/support/v7/appcompat/R.styleable.html#Toolbar
你可以在https://developer.android.com/reference/android/support/v7/appcompat/R.styleable.html#Toolbar 中找到它们的细节元素
Here are some more:TextAppearance.Widget.AppCompat.Toolbar.Title
, TextAppearance.Widget.AppCompat.Toolbar.Subtitle
, Widget.AppCompat.Toolbar.Button.Navigation
.
这里还有一些:TextAppearance.Widget.AppCompat.Toolbar.Title
, TextAppearance.Widget.AppCompat.Toolbar.Subtitle
, Widget.AppCompat.Toolbar.Button.Navigation
。
Hope this can help you.
希望这可以帮到你。
回答by craigmj
Similar to Arnav Rao's, but with a different parent:
类似于 Arnav Rao 的,但具有不同的父级:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="toolbarStyle">@style/MyToolbar</item>
</style>
<style name="MyToolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:background">#ff0000</item>
</style>
With this approach, the appearance of the Toolbar is entirely defined in the app styles, so you don't need to place any styling on each toolbar.
使用这种方法,工具栏的外观完全在应用程序样式中定义,因此您无需在每个工具栏上放置任何样式。