简体   繁体   English

Android自定义AppCompat ActionBar

[英]Android customize AppCompat ActionBar

I have seen multiple threads on this but I am unable to find the solution to my problem. 我已经看到了多个线程,但是找不到我的问题的解决方案。 I want to create a custom action bar so to begin I just tried something simple like just a custom color: 我想创建一个自定义操作栏,因此,我首先尝试了一些简单的操作,例如自定义颜色:

<style name="CustomActionBarTheme"
    parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar"
    parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">#FF0000</item>
</style>

Then in my Manifest I have: 然后在Manifest我有:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/CustomActionBarTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >

My MainActivity also extends ActionBarActivity 我的MainActivityextends ActionBarActivity

I was getting an error saying that I need to apply Theme.AppCompat to my Activity but I am no longer getting that error. 我收到一条错误消息,说我需要将Theme.AppCompat应用于我的Activity但我不再遇到该错误。 My application compiles and runs but the action bar is just standard black, not red (#FF0000). 我的应用程序可以编译并运行,但操作栏只是标准的黑色,而不是红色的(#FF0000)。

Eventually I want to make my action bar transparent but I figured I would get this sorted out first. 最终,我想使操作栏透明,但我认为我首先要弄清楚这一点。 Any help would be appreciated. 任何帮助,将不胜感激。

As said in the documentation , for retro-compatibility, you should use actionBarStyle along with android:actionBarStyle : 文档中所述,为了实现复古兼容性,您应该将actionBarStyleandroid:actionBarStyle一起使用:

<style name="CustomActionBarTheme"
    parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar"
    parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">#FF0000</item>
</style>

If you want flexibility use Toolbar instead of ActionBar. 如果需要灵活性,请使用工具栏而不是ActionBar。 I dont think you can make the default action bar transpatent, and you can achieve that with Toolbar. 我不认为您可以使默认的操作栏透明化,而可以通过工具栏实现。 Read more here http://android-developers.blogspot.de/2014/10/appcompat-v21-material-design-for-pre.html 在此处阅读更多信息http://android-developers.blogspot.de/2014/10/appcompat-v21-material-design-for-pre.html

使用新的AppCompat-v7(v21),ActionBar样式的属性需要引用应用程序名称空间,即没有android:前缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM