c# - 如何在 Xamarin android 中更改操作栏标题颜色

标签 c# xamarin android-actionbar xamarin.android

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

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

这是我的代码:

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);
            }
        }
    }
}

最佳答案

最直接的方法是为您的 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样式的讨论很多,你可以搜索一下。

I don't want to switch to toolbar, because I don't know the code for it, I also dont know if it's possible to make a toolbar look like exactly the same as the actionbar in the code below.

我个人认为使用Toolbar自定义布局更容易,但是从android 5.0开始可以使用,关于Toolbar的代码部分,您可以引用Toolbar .

关于c# - 如何在 Xamarin android 中更改操作栏标题颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41550025/

相关文章:

javascript - 删除挖空 View 模型的空属性

android - 如何在滚动后恢复 ViewGroup 对 TouchEvents 的处理

java - 滚动 RecyclerView 时出现滞后/错误的 y 坐标(OnscrollListener)

c# - MVC : what code gets called when you click the "submit" button?

c# - 继承流;在 Read() 中缓冲

layout - xamarin.Forms 仅滚动页面的一部分

xaml - Xamarin 表单(跨平台): Multiple type of cells in ListView

java - 这个怎么修??总是要求更改构造函数 --> mAdapter = new TabsPagerAdapter(getFragmentManager());

android-layout - 如何防止键盘将操作栏推离屏幕

c# - 如何重置我的 C# 程序(Windows 移动版)?