改变Android的导航栏冠军xamarin.forms项目

问题描述:

我试图改变我的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

只有标题文字颜色 –

我试图改变我的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> 
  1. 集活动主题
  2. 添加自定义样式:

    [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中......但是当你说“添加自定义样式”时,我不知道我在哪里放置了这个代码 –