本文介绍了更改操作栏颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更改操作栏的颜色,但是当我在Android应用程序中更改这些数据时,它不会动态更改颜色(与iOS中不同).

I need to change the colors of the action bar, but when I change these data in the Android app, it does not change colors dynamically (unlike in iOS).

当我将操作栏的默认颜色更改为白色(背景颜色)时,我需要系统将自动更改标题并将箭头颜色返回为黑色(它们是白色且不可见).如上所述,在iOS应用中,仅使用此代码,系统已经自动进行此切换,但是对于Android,我不知道该如何实现同样的效果.

I need when I change the default color of the action bar to white (background color) the system will automatically change the title and return arrow colors to black (they are white and invisible). As I said above, in the iOS app only with this code the system already makes this switch automatically, however for Android I do not know how to do this same effect.

遵循以下代码:

private void ChangeActionBarColor()
{
    var navigationPage = Detail as NavigationPage;
    navigationPage.BarBackgroundColor = Ambiente.Instance.NavigationColor;
    navigationPage.BarTextColor = Ambiente.Instance.NavigationItemColor;
    Title = Ambiente.Instance.NavigationTitle;
}

推荐答案

您需要在droid/resource/styles.xml中创建自定义主题.并且还需要在您的MainActivity中应用此主题

You need to create a custom theme in droid/resource/styles.xml. and also need to apply this theme in your MainActivity

在MainActivity上应用主题

[Activity(Label = "YourProject.Droid", Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity

在Style.XML文件中添加自定义主题

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <style name="MyTheme" parent="MyTheme.Base">
    </style>
    <!-- Base theme applied no matter what API -->
    <style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        <!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
        <item name="windowNoTitle">true</item>
        <!--We will be using the toolbar so no need to show ActionBar-->
        <item name="windowActionBar">false</item>
        <!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette-->
        <!-- colorPrimary is used for the default action bar background -->
        <item name="colorPrimary">#2196F3</item>
        <!-- colorPrimaryDark is used for the status bar -->
        <item name="colorPrimaryDark">#1976D2</item>
        <!-- colorAccent is used as the default value for colorControlActivated
             which is used to tint widgets -->
        <item name="colorAccent">#FF4081</item>
        <!-- You can also set icon, Title color -->
        <item name="android:textColorPrimary">#000000</item>
        <!-- You can also set colorControlNormal, colorControlActivated
             colorControlHighlight and colorSwitchThumbNormal. -->
        <item name="windowActionModeOverlay">true</item>
        <item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
        <item name="android:actionBarPopupTheme">@style/CustomActionBarPopupTheme</item>
    </style>
    <style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
        <item name="colorAccent">#FF4081</item>
    </style>
    <style name="CustomActionBarPopupTheme" parent="android:ThemeOverlay.Material.Light">
        <item name="android:colorBackground">#FFFFFF</item>
        <item name="android:textColor">#000000</item>
    </style>
</resources>

获得更多了解

这篇关于更改操作栏颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:18