[英]Changing android nav bar title in xamarin.forms project
I try to change android nav bar title of my xamarin forms project 我尝试更改我的Xamarin Forms项目的android导航栏标题
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: 我不能在im.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>
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': 我的“ 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: 如果将主题更改为MyTheme,则会发生以下异常:
Unhandled Exception: Java.Lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 未处理的异常:Java.Lang.IllegalStateException:您需要在此活动中使用Theme.AppCompat主题(或后代)。
I try to change android nav bar title of my xamarin forms project 我尝试更改我的Xamarin Forms项目的android导航栏标题
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. 正如Rendy Del Rosario所说的那样 ,将主题设置为Android Resources/values
内的Styles.xml
定义的MainActivity
,而不是将此样式放入清单中。
Styles.xml
inside Android Resources/values
在Android Resources/values
内的Styles.xml
定义 Your 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>
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. 未处理的异常:Java.Lang.IllegalStateException:您需要在此活动中使用Theme.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 . 我读了您的MainActivity
代码,因为您尝试将对话框主题应用到的活动正在扩展AppCompatActivity
,而该活动要求应用AppCompat
主题 ,所以会发生此错误。 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
: 由于您在MainActivity
中使用工具栏:
ToolbarResource = Resource.Layout.Toolbar;
The nav bar title in Android is Toolbar
, so you just need to change the toolbar title color. Android中的导航栏标题为Toolbar
,因此您只需要更改工具栏标题的颜色。
1.Using the default theme when you create a Xamarin.Android
project : 1.创建Xamarin.Android
项目时使用默认主题:
Resources/values/Styles.xml
: 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
: 2.添加一个ToolbarTheme
到您的Toolbar
:
Define a ToolbarTheme
, place it in Resources/values/Styles.xml
file, you could see it in the above code, I write here again : 定义一个ToolbarTheme
,将其放置在Resources/values/Styles.xml
文件中,您可以在上面的代码中看到它,我再次在这里写道:
<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. android:textColorPrimary
是导航栏标题颜色,将其替换为要更改的颜色。
Find your Toolbar.axml
file : 找到您的Toolbar.axml
文件:
Modify your Toolbar
theme to : 将Toolbar
主题修改为:
android:theme="@style/ToolbarTheme"
Complete Toolbar.axml
code : 完整的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" />
Effect : 效果:
When I change the toolbar title color to red : 当我将工具栏标题颜色更改为红色时:
<item name="android:textColorPrimary">#ff0000</item>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.