2017-07-11 34 views
5
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); 

Powyższy kod nie zmienia koloru strzałki. Wartość parametru upArrow ColorFilter ma wartość NULL. Jaki powinien być tego powód? Nie pytam, jak zmienić kolor do rysowania. Moje pytanie brzmi: dlaczego powyższy kod nie ustawił filtru kolorów? Poniżej znajduje się kod MainActivity.SetColorFilter nie działa w Xamarin.Android

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<IMvxViewPresenter>() as MvxFormsDroidMasterDetailPagePresenter; 
     var presenter = Mvx.Resolve <IMvxViewPresenter>() as MVxFormsDroidCustomPagePresenter;//MvxFormsDroidPagePresenter; 
     if (presenter == null) { 
      throw new ArgumentNullException (nameof(presenter), "MvxFormsApplicationActivity: Please check your Activity class and ensure the presenter has value"); 
     } 
     //presenter.MvxFormsApp = mvxFormsApp; 
     presenter.MvxFormsApp = oznesFormsApp; 
     Mvx.Resolve<IMvxAppStart>().Start(); 
    } 
    } 
+0

Możliwy duplikat [Jak zmienić kolory rozciągliwej w Androidzie?] (Https://stackoverflow.com/questions/1309629/how-to- zmienić kolory rysowania w systemie android) – Cheesebaron

+0

@Cheesebaron Nie! Nie chodzi o to, o to, dlaczego nie działa. – Heshan

+0

"Wartość parametru upArrow ColorFilter jest null.", Użyłem twojego kodu do testowania, nie jest on zerowy przy moim boku. Jaka jest Twoja docelowa wersja Androida? –

Odpowiedz

3

Oto minimalna próba wykazania SetColorFilter działa na Android 7.1 i Android 6.0 emulatora.

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<Button>(Resource.Id.button); 
     b.Click += B_Click; 
    } 

    private void B_Click(object sender, System.EventArgs e) 
    { 
     Intent i = new Intent(this, typeof(MainActivity)); 
     StartActivity(i); 
    } 
} 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="App39.App39" android:versionCode="1" android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="16" /> 
    <application android:label="App39" android:theme="@style/Theme.AppCompat.Light"></application> 
</manifest> 

Jaka jest różnica między moim kodu i kodu?

Używasz dwóch nieaktualnych metod, które w rzeczywistości powinieneś używać odpowiednika biblioteki pomocniczej. Takich jak ContextCompat.GetColor. Po drugie powinieneś używać metody AppCompatResources lub ResourcesCompatGetDrawable zamiast Resources.GetDrawable.

https://developer.android.com/reference/android/support/v7/content/res/AppCompatResources.html#getDrawable(android.content.Context, int)

https://developer.android.com/reference/android/support/v4/content/res/ResourcesCompat.html#getDrawable(android.content.res.Resources, int, android.content.res.Resources.Theme)

Android API 25

+0

Moja główna działalność wywodzi się z FormsAppCompatActivity i podaje następujący błąd podczas próby użycia kodu. Java.Lang.NoSuchMethodError: brak metody o nazwie = 'getDrawable' signature = '(I) Landroid/graphics/drawable/Drawable;' w klasie Landroid/content/Context; – Heshan

+0

Upewnij się, że kompilujesz> = API 21: https://developer.android.com/reference/android/content/Context.html#getDrawable(int) –

+0

Zaktualizowałem pytanie, dodając więcej kodu. Proszę spojrzeć. – Heshan