如何以编程方式更改 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/