2017-10-09 55 views
0

我試圖改變我的xamarin形式的android導航欄名稱項目改變Android的導航欄冠軍xamarin.forms項目

我不能這樣做IM的App.xaml因爲當我在PCL改變顏色它改變IOS備份按鈕過了,我不想它...然後我已經嘗試過:

<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> 

我讀,我需要把某些東西的清單,但我沒有找到如何要做到這一點...

沒有錯誤,沒有什麼變化......

我 'MainActivity':

namespace neoFly_Montana.Droid 
{ 

[Activity(Label = "neoFly_Montana", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] 
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity 
{ 
    public static Context AppContext; 
    protected override void OnCreate(Bundle bundle) 
    { 
     TabLayoutResource = Resource.Layout.Tabbar; 
     ToolbarResource = Resource.Layout.Toolbar; 

     base.OnCreate(bundle); 

     global::Xamarin.Forms.Forms.Init(this, bundle); 

     //qrcode inicializa 
     ZXing.Net.Mobile.Forms.Android.Platform.Init(); 

     //inicializa imageCircle 
     ImageCircleRenderer.Init(); 

     //inicializa o mapa 
     global::Xamarin.FormsMaps.Init(this, bundle); 

     //shared Preferences 
     App.Init(new AndroidUserPreferences()); 

     //Gerenciador de memória 
     CachedImageRenderer.Init(); 

     //AndroidUserPreferences sharedPref = new AndroidUserPreferences(); 
     //var token = FirebaseInstanceId.Instance.Token; 
     //sharedPref.SetString("token", token); joyce descomentar 

     LoadApplication(new App()); 
    } 

    // Field, property, and method for Picture Picker 
    public static readonly int PickImageId = 1000; 

    public TaskCompletionSource<Stream> PickImageTaskCompletionSource { set; get; 
} 

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent) 
    { 
     base.OnActivityResult(requestCode, resultCode, intent); 

     if (requestCode == PickImageId) 
     { 
      if ((resultCode == Result.Ok) && (intent != null)) 
      { 
       Android.Net.Uri uri = intent.Data; 
       Stream stream = ContentResolver.OpenInputStream(uri); 

       // Set the Stream as the completion of the Task 
       PickImageTaskCompletionSource.SetResult(stream); 
      } 
      else 
      { 
       PickImageTaskCompletionSource.SetResult(null); 
      } 
     } 
    } 

    //qrcode permission 
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults) 
    { 
     global::ZXing.Net.Mobile.Forms.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults); 
    }  

} 
} 

如果我改變主題的MyTheme此異常ocures:

未處理的異常:Java.Lang.IllegalStateException :您需要在此活動中使用Theme.AppCompat主題(或後代)。

+0

究竟是你想改變什麼?你想只改變標題文字顏色,但不改變其他文字顏色?如果你在你的問題中表達了這一點,而不是讓我們猜測,那將會非常有幫助。 – Jason

+0

只有標題文字顏色 –

回答

0

我試圖改變我的xamarin形式的android導航欄名稱項目

由於Rendy Del Rosario said,設置在清單中Styles.xml裏面的Android Resources/values,而不是把這種風格定義爲MainActivity主題。

  1. 定義於一個Styles.xml內部的Android Resources/values

enter image description here

Styles.xml

<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 = "WorkingWithNavigation.Droid", Theme = "@style/MyTheme", 
        Icon = "@drawable/icon", 
        MainLauncher = true, 
        ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] 
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity 
    { 
        ... 
    } 
    

    效果:

    Use themeNo theme

    編輯:

    未處理的異常:Java.Lang.IllegalStateException:您需要使用Theme.AppCompat主題(或後代)與本次活動。

    我看了你的MainActivity代碼,因爲你嘗試應用對話主題活動正在擴展其AppCompatActivity需要AppCompat的主題應用出現此錯誤。爲了解決這個問題並實現你的功能,我找到了另一個解決方案,它在我身邊很好地工作。

    由於您使用的工具欄在您的MainActivity:在Android的

    ToolbarResource = Resource.Layout.Toolbar; 
    

    導航欄標題是Toolbar,那麼你只需要改變工具欄標題顏色。

    1.使用默認的主題,當你創建一個Xamarin.Android項目:

    Resources/values/Styles.xml

    <?xml version="1.0" encoding="utf-8" ?> 
    <resources> 
    
        <style name="ToolbarTheme" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 
        <item name="android:textColorPrimary">#ff0000</item> 
        </style> 
    
        <style name="MainTheme" parent="MainTheme.Base"> 
        </style> 
    
        <style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar"> 
        <item name="windowNoTitle">true</item> 
        <item name="windowActionBar">false</item> 
        <item name="colorPrimary">#2196F3</item> 
        <item name="colorPrimaryDark">#1976D2</item> 
        <item name="colorAccent">#FF4081</item> 
        <item name="windowActionModeOverlay">true</item> 
        <item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item> 
        </style> 
    
        <style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog"> 
        <item name="colorAccent">#FF4081</item> 
        </style> 
    </resources> 
    

    2.增加一個ToolbarThemeToolbar

    定義一個ToolbarTheme,將其放置在Resources/values/Styles.xml文件,你可以看到它在上面的代碼中,我再次寫在這裏:

    <style name="ToolbarTheme" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 
        <item name="android:textColorPrimary">#ff0000</item> 
    </style> 
    

    android:textColorPrimary是導航欄標題顏色,你想改變顏色替換它。

    找到你Toolbar.axml文件:

    enter image description here

    修改您Toolbar主題:

    android:theme="@style/ToolbarTheme" 
    

    完全Toolbar.axml代碼:

    <?xml version="1.0" encoding="utf-8"?> 
    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/toolbar" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="?attr/colorPrimary" 
        android:theme="@style/ToolbarTheme" 
        android:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 
    

    效果:

    當我改變工具欄標題顏色爲紅色:

    <item name="android:textColorPrimary">#ff0000</item> 
    

    enter image description here

    +0

    我在哪裏放第二個代碼? –

    +0

    @Joyce de Lanna,你的意思是你的「資源」代碼? –

    +0

    對不起,我不明白...你發佈的第一個關於樣式的代碼我知道我應該把它放在資源文件夾中的style.xml中......但是當你說「添加自定義樣式」時,我不知道我在哪裏放置了這個代碼 –