更改ActionBar颜色 - Xamarin

7

关于 Android 中的 ActionBar ,我有一个小问题。

我正在使用 Xamarin Studio 制作应用程序,希望改变 ActionBar 的颜色。我已经找了很久想要改变颜色的方法,但这是我能找到的唯一方法。

ColorDrawable colorDrawable = new ColorDrawable(Color.ParseColor("#ff0f62ae"));
            this.ActionBar.SetBackgroundDrawable(colorDrawable); 

我原本以为这会起作用,但现在我遇到了异常,而且我无法弄清楚为什么它对我不起作用。

这是异常信息:

Object reference not set to an instance of an object

有人能解释一下我做错了什么,然后告诉我怎么修复吗?

编辑

在几条评论的指导下,我已经尝试过以下方法:

<resources>
    <style name="AppTheme"
           parent="android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">@color/</item>
        <item name="andriod:titleTextStyle">@style/MyActionBarTextAppearance</item>
    </style>

    <style name="MyActionBarTextAppearance" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
       <item name="android:textColor">@color/default_white</item>
    </style>
</resources>

我该如何添加自定义颜色,以及如何将其分配给我的操作栏?


我认为你可以创建一个样式,参考 https://developer.android.com/training/basics/actionbar/styling.html - PedroAGSantos
我认为上述操作是可行的,因为有人在这里尝试过:https://dev59.com/DGbWa4cB1Zd3GeqPatBm 有人说它可以工作。 - user3996249
是的,它可能有效,但如果您有一个可以使一切变得清晰的模式,我认为您可以尝试。如果不起作用,我们需要看看原因是什么?但请尝试创建您自己的风格。 - PedroAGSantos
1
ActionBar bar = getActionBar(); bar.setBackgroundDrawable(new ColorDrawable(Color.BLUE));动作栏 bar = getActionBar(); bar.setBackgroundDrawable(new ColorDrawable(Color.BLUE)); - PedroAGSantos
getActionBar对我不起作用。 - user3996249
看,这个教程可能有点旧,但我认为你可以试一下:http://diptimayapatra.wordpress.com/2013/07/08/xamarin-adding-custom-title-bar-in-android/。 - PedroAGSantos
6个回答

3
我是这样做的。请尝试一下,如果有效,请告诉我。
<style name="AppTheme" parent="android:Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/ActionBarTheme</item>
</style>

<style name="ActionBarTheme" parent="android:Widget.Holo.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/color_action_bar</item>
</style> "

在colors.xml中或者strings.xml中添加颜色变量,为操作栏添加所需的颜色。
<color name="color_action_bar">#3450A3</color>

在清单文件中提及主题。
<application
    android:theme="@style/AppTheme" >

非常感谢您的回答,我会尝试的。 - user3996249
@Mayank 请确保在您的自定义应用程序主题中,windowActionBar已关闭并且NoTitle已打开。<item name="android:windowActionBar">false</item> <item name="android:windowNoTitle">true</item> - BYISHIMO Audace

1
Try this:


<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">#f00</item>
        <item name="android:titleTextStyle">@style/MyTextAppearance</item>
    </style>

    <style name="MyTextAppearance" parent="android:TextAppearance.Holo.Widget.ActionBar.Title">
        <item name="android:textColor">#ffffff</item>
    </style>

3
我应该在程序的哪里和如何包含这个? - user3996249
我需要创建styles.XML并将其放入资源文件夹中,还是将其放入另一个文件夹中? - user3996249

0
作为对 @SID 答案的解释,要为您的 Actionbar 进行样式设置,您必须覆盖应用程序的整个主题。
如果它们不存在,请在“资源>值”文件夹下添加 styles.XML 和 colors.XML 文件,并将它们编辑如下:

Styles.XML:

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <style name="AppTheme" parent="android:Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/ActionBarTheme</item>
  </style>

  <style name="ActionBarTheme" parent="android:Widget.Holo.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/DarkBlue</item>
  </style>
</resources>

Colors.XML:

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <color name="DarkBlue">#007acc</color> 
</resources>

现在导航到 属性>AndroidManifest.xml 并更改 <application> 标签以引用您的自定义主题:

  <application 
    android:theme="@style/AppTheme" 
     ... >

  </application>

更新:

如果您愿意使用AppCompatActionBarActivity,那么您将不再需要在Styles.XML中使用ActionBarTheme,因此这是Styles.XML的样式:

<?xml version="1.0" encoding="utf-8" ?>
<resources>

  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/LightBlue</item>
    <item name="colorPrimaryDark">@color/DarkBlue</item>
    <item name="colorAccent">@color/Pink</item>
  </style>

</resources>

还有 Colors.XML

<?xml version="1.0" encoding="utf-8" ?>
<resources>

  <color name="DarkBlue">#0052cc</color>
  <color name="Pink">#ff1ac6</color>
  <color name="LightBlue">#1a75ff</color>

</resources>

0

这对我有用

   <color name="actionBarBackgroundColor">#2C3E50</color>
    <color name="actionBarTextColor">#FFFFFF</color>
  <style name="MyAppTheme"/>
    <style name="MyAppTheme" parent="@android:style/Theme.DeviceDefault.Light">
      <item name="android:actionBarStyle">@style/MyTheme.ActionBarStyle</item>         
    </style>
    <style name="MyTheme.ActionBarStyle" parent="@android:style/Widget.DeviceDefault.Light.ActionBar">
      <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
      <item name="android:background">@color/actionBarBackgroundColor</item>
    </style>
    <style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Title">
      <item name="android:textColor">@color/actionBarTextColor</item>
    </style>

0

你的问题就是我的解决方案,我使用了你的代码并进行了一些微小的修改,它成功运行了。下面是你的代码和我修改后的版本,附上了结果代码的图片示例enter image description here

//Define ActionBar
       actionbar=SupportActionBar;
//Define Color Drawable
            ColorDrawable colorDrawable = new ColorDrawable(Color.ParseColor("#ff0f62ae"));
//Set drawable color to ActionBar
    actionbar.SetBackgroundDrawable(colorDrawable);

-1
一个非常简单的解决方案,进入你的Xamarin.Android项目中的"styles.xml"文件,然后只需将以下属性替换或添加到你的样式中即可:
<!-- colorPrimary is used for the default action bar background -->    
<item name="colorPrimary">#002500</item>
<!-- colorPrimaryDark is used for the status bar #4FAB5B-->
<item name="colorPrimaryDark">#002500</item>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接