var upArrow = Resources.GetDrawable(Resource.Drawable.abc_ic_ab_back_material);
upArrow.SetColorFilter(Resources.GetColor(Android.Resource.Color.White),PorterDuff.Mode.SrcIn);
SupportActionBar.SetHomeAsUpIndicator(upArrow);
上面的代码不会改变箭头颜色. upArrow ColorFilter值为null.这应该是什么原因?我不是问如何改变可绘制的颜色.我的问题是为什么上面的代码无法设置滤色器?以下是MainActivity代码.
public class MvxFormsApplicationActivity : FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
Xamarin.Forms.Forms.Init(this,bundle); ;
//Plugins.NewictLib.Forms.Android.Renderers.GifImageViewRenderer.Init ();
DLToolkit.Forms.Controls.FlowListView.Init ();
UserDialogs.Init ((Activity) Xamarin.Forms.Forms.Context);
OxyPlot.Xamarin.Forms.Platform.Android.PlotViewRenderer.Init();
// var mvxFormsApp = new MvxFormsApp ();
// LoadApplication (mvxFormsApp);
var myApp = new MyFormsApp();
LoadApplication (myApp );
AppCompatDelegate.CompatVectorFromResourcesEnabled = true;
//the following needs to set so that the back button color can be changed
var upArrow = Resources.GetDrawable(Resource.Drawable.abc_ic_ab_back_material);
upArrow.SetColorFilter(Resources.GetColor(Android.Resource.Color.White),PorterDuff.Mode.SrcIn);
SupportActionBar.SetHomeAsUpIndicator(upArrow);
if (IsPlayServicesAvailable())
{
var intent = new Intent(this,typeof(RegistrationIntentService));
StartService(intent);
}
//var presenter = Mvx.Resolve
最佳答案
这是演示在Android 7.1和Android 6.0模拟器上运行的SetColorFilter的最小示例.
MainActivity.cs
[Activity(Label = "App39",MainLauncher = true,Icon = "@drawable/icon")]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
var upArrow = AppCompatResources.GetDrawable(this,Resource.Drawable.abc_ic_ab_back_material);
upArrow.SetColorFilter(new Color(ContextCompat.GetColor(this,Android.Resource.Color.HoloBlueBright)),PorterDuff.Mode.SrcIn);
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
SupportActionBar.SetHomeAsUpIndicator(upArrow);
Button b = FindViewById
AndroidManifest.xml中
我的代码和代码之间有什么区别?
您正在使用两种已弃用的方法,实际上,您应该使用等效的支持库.例如ContextCompat.GetColor.其次,您应该使用AppCompatResources或ResourcesCompat GetDrawable方法而不是Resources.GetDrawable.