在xamarin.forms项目中更改android导航栏标题 [英] Changing android nav bar title in xamarin.forms project

查看:128
本文介绍了在xamarin.forms项目中更改android导航栏标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试更改我的xamarin表单项目的android导航栏标题

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

我无法执行im app.xaml,因为当我在pcl中更改颜色时,它也会更改ios后退按钮,而且我也不想...然后我已经尝试过:

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

我的"MainActivity":

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

}
}

如果将主题更改为MyTheme,则发生此异常:

If I change the theme to MyTheme this exception ocures:

未处理的异常:Java.Lang.IllegalStateException:您需要在此活动中使用Theme.AppCompat主题(或后代).

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

推荐答案

我尝试更改我的xamarin表单项目的android导航栏标题

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

作为 Rendy Del Rosario说,将主题设置为在Android Resources/values内部的Styles.xml中定义的MainActivity,而不是将这种样式放入清单中.

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

  1. 在Android Resources/values
  2. 中的Styles.xml中定义

您的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. 设置活动主题

添加您的自定义样式:

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

效果:

使用主题

未处理的异常:Java.Lang.IllegalStateException:您需要在此活动中使用Theme.AppCompat主题(或后代).

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

我阅读了您的MainActivity代码,发生此错误是因为您试图将对话框主题应用于的活动正在扩展AppCompatActivity,这需要应用AppCompat主题.为了解决此问题并实现您的功能,我找到了另一种解决方案,并且对我而言效果很好.

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.

因为您正在使用MainActivity中的工具栏:

Since you are using toolbar in your MainActivity :

ToolbarResource = Resource.Layout.Toolbar;

Android中的导航栏标题为Toolbar,因此您只需要更改工具栏标题的颜色.

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

1.创建Xamarin.Android项目时使用默认主题:

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.向Toolbar中添加ToolbarTheme:

2.Add a ToolbarTheme to your Toolbar :

定义一个ToolbarTheme ,将其放置在Resources/values/Styles.xml文件中,您可以在上面的代码中看到它,我再次在这里写道:

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>

android:textColorPrimary是导航栏标题颜色,将其替换为要更改的颜色.

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

找到您的Toolbar.axml文件:

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

效果:

当我将工具栏标题颜色更改为红色:

When I change the toolbar title color to red :

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

这篇关于在xamarin.forms项目中更改android导航栏标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆