简体   繁体   中英

Changing android nav bar title in xamarin.forms project

I try to change android nav bar title of my xamarin forms project

I can't do that im app.xaml because when i change the color in pcl it changes ios back button too, And I don't wanna it...then I have already tried:

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

I read that I need to put something in the manifest but I Didn't found how to do that...

no error and nothing change...

My '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);
    }    

}
}

If I change the theme to MyTheme this exception ocures:

Unhandled Exception: Java.Lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

I try to change android nav bar title of my xamarin forms project

As Rendy Del Rosario said , setting a theme to MainActivity defined in a Styles.xml inside Android Resources/values instead of put this style in the manifest.

  1. Defined in a Styles.xml inside Android Resources/values

在此处输入图片说明

Your 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. Set activity theme

Add your custom style :

[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
{
    ...
}

Effect :

Use theme , No theme .

EDIT :

Unhandled Exception: Java.Lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

I read your MainActivity code, this error occurs because the activity you are trying to apply the dialog theme to is extending AppCompatActivity which requires the AppCompat theme to be applied . To solve this problem and implement your feature I found another solution and it works fine on my side.

Since you are using toolbar in your MainActivity :

ToolbarResource = Resource.Layout.Toolbar;

The nav bar title in Android is Toolbar , so you just need to change the toolbar title color.

1.Using the default theme when you create a Xamarin.Android project :

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.Add a ToolbarTheme to your Toolbar :

Define a ToolbarTheme , place it in Resources/values/Styles.xml file, you could see it in the above code, I write here again :

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

The android:textColorPrimary is the nav bar title color, replace it with the color you want to change.

Find your Toolbar.axml file :

在此处输入图片说明

Modify your Toolbar theme to :

android:theme="@style/ToolbarTheme"

Complete Toolbar.axml code :

<?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" />

Effect :

When I change the toolbar title color to red :

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM