如何在Xamarin android

时间:2017-01-09 14:20:18

标签: c# xamarin android-actionbar xamarin.android

如何以编程方式更改Xamarin android中的操作栏标题颜色? 我在互联网上尝试了很多东西,但我无法找到办法。

我不想切换到工具栏,因为我不知道它的代码,我也不知道是否可以使工具栏看起来与以下代码中的操作栏。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Support.V7.App;
using Android.Graphics.Drawables;
using Android.Graphics;

namespace actionbarTitleColor
{
    [Activity(Label = "myActivity")]
    public class myActivity : Activity
    {
        protected override void OnCreate(Bundle bundle) {

            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Rapsolie);
            ActionBar.SetHomeButtonEnabled(true);
            ActionBar.SetDisplayHomeAsUpEnabled(true);
            ColorDrawable colorDrawable = new ColorDrawable(Color.ParseColor("#00b8ff"));
            ActionBar.SetBackgroundDrawable(colorDrawable);

        }

        public override bool OnOptionsItemSelected(IMenuItem item) {

            switch (item.ItemId) {
                case Android.Resource.Id.Home:
                    Finish();
                    return true;

                default:
                    return base.OnOptionsItemSelected(item);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:4)

最直接的方法是为ActionBar自定义样式,例如:

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

  <style name="MyTheme.ActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
  </style>

  <style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">#f08080</item>
  </style>
</resources>

然后在您的页面中应用此样式,如下所示:

[Activity(Label = "YOURPACKAGENAME", MainLauncher = true, Icon = "@drawable/icon", Theme = "@style/MyTheme")]

有很多关于自定义ActionBar样式的讨论,你可以搜索它们。

  

我不想切换到工具栏,因为我不知道它的代码,我也不知道是否可以使工具栏看起来与下面代码中的操作栏完全相同。

我个人认为使用Toolbar对于自定义布局更容易,但是从Android 5.0开始可用,对于如何为Toolbar编码的部分,您可以参考Toolbar