【发布时间】:2014-07-22 11:32:18
【问题描述】:
在 Xamarin 中,如何更改 Fragment 中的 ActionBar 背景颜色和文本颜色?
这是在Activity 中工作的代码:
ColorDrawable colorDrawable = new ColorDrawable(Color.White);
ActionBar.SetBackgroundDrawable(colorDrawable);
int titleId = Resources.GetIdentifier("action_bar_title", "id", "android");
TextView abTitle = (TextView) FindViewById(titleId);
abTitle.SetTextColor (Color.Black);
如果我在 Fragment 中对同一个项目有相同的代码,我会收到以下错误:
非静态字段、方法或 财产 'Android.App.ActionBar.SetBackgroundDrawable(Android.Graphics.Drawables.Drawable)'
在这行代码处:
ActionBar.SetBackgroundDrawable(colorDrawable);
如果我注释掉上面的代码行,我会得到这个错误:
System.NullReferenceException:对象引用未设置为实例 一个对象
在这行代码处:
abTitle.SetTextColor (Color.Black);
另外,我将此代码放在OnCreateView 函数中。
需要如何更改代码以使其在Fragment 而非Activity 中工作?
提前致谢
【问题讨论】:
标签: android android-fragments colors android-actionbar xamarin