【问题标题】:Xamarin Forms: How to change the color the "back" symbol in the toolbarXamarin Forms:如何更改工具栏中“后退”符号的颜色
【发布时间】:2018-11-27 15:59:13
【问题描述】:

我想更改工具栏上的后退符号的颜色,目前工具栏的颜色为黑色。为了视觉清晰,这里是左上角带有“后退”箭头的屏幕截图。如屏幕截图所示,我已经更改了文本和背景的颜色。

Screenshot: Contentpage with black "back" arrow

项目分为ClientClient.AndroidClient.iOS。 该应用程序目前主要面向 Android,但我们希望为 iOS 敞开大门。

如果有任何遗漏,我也会尝试将其粘贴在这里。

MasterDetailsPage.xaml.cs:

   public partial class MasterDetailPage1: MasterDetailPage
    {
        public MasterDetailPage1()
        {
            this.InitializeComponent();
            this.MasterPage.ListView.ItemSelected += this.ListView_ItemSelected;
        }

        protected override void OnChildAdded(Element child)
        {
            if(child is NavigationPage page)
            {                 
                page.BarBackgroundColor = Color.FromHex("343A40");
                page.BackgroundColor = Color.FromHex("343A40");
                page.BarTextColor = Color.FromHex("FFFFFF");
            }

            base.OnChildAdded(child);
        }
}

ReportConfigurationPage.xaml.cs

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="Company.Client.Views.ReportConfigurationPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <ContentPage.Content>
        <ScrollView x:Name="ScrollView" BackgroundColor="White">
            <StackLayout x:Name="BaseStackLayout" Orientation="Vertical">     
            <!-- Input controls -->
            </StackLayout>
        </ScrollView>
    </ContentPage.Content>
</ContentPage>

更新: 啊,谢谢,需要在styles.xml中定义和分配的是DrawerArrowStyle

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <style name="DrawerArrowStyle" parent="@style/Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">true</item>
    <item name="color">#FFFFFF</item>
  </style>

  <style name="MainTheme" parent="MainTheme.Base">
  </style>
  <!-- Base theme applied no matter what API -->
  <style name="MainTheme.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">#FF0000</item>
    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">#219198</item>
    <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
    <item name="colorAccent">#219198</item>
    <!-- You can also set colorControlNormal, colorControlActivated
         colorControlHighlight and colorSwitchThumbNormal. -->
    <item name="colorControlHighlight">#219198</item>
    <item name="windowActionModeOverlay">true</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    <item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
  </style>

  <style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">#FF4081</item>
  </style>
</resources>

【问题讨论】:

标签: c# xamarin xamarin.forms xamarin.android


【解决方案1】:

Android 中有一个概念叫做Material design

由于 Xamarin 在 Xamarin.Android 中采用了本机 Java Android 行为,Android 应用程序会在其 styles.xml 文件中选择主题并使用该样式设置栏背景颜色。

当然,有一种解决方法。无论您需要在 Android 端进行什么更改,您都必须在样式文件中对其进行更新,例如:

 <?xml version="1.0" encoding="utf-8" ?>
 <resources>
 <style name="MyTheme" parent="MyTheme.Base">
   </style>

 <style name="DrawerArrowStyle" parent="@style/Widget.AppCompat.DrawerArrowToggle">
 <item name="spinBars">true</item>
 <item name="color">#FFFFFF</item>
 </style>

 <style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
 <item name="windowNoTitle">true</item>
 <item name="windowActionBar">false</item>
 <item name="colorPrimary">#003399</item>
 <item name="colorPrimaryDark">#003399</item>
 <item name="colorControlHighlight">#003399</item>
 <item name="colorAccent">#012348</item>
 <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
 </style>

 </resources>

这里的颜色变化将直接反映在那里,例如 ColorPrimary 是您的工具栏背景颜色 (BarBackgroundColor)。

更新

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:id="@+id/toolbar"
 android:minHeight="?attr/actionBarSize"
 android:background="?attr/colorPrimary"
 app:theme="@style/ToolbarTheme" >
</android.support.v7.widget.Toolbar>

然后得到这样的工具栏:

var toolbar=yourActivityContext.Window.DecorView.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
toolbar.SetBackgroundColor(Android.Graphics.Color.Your_Color);
//In case of hex color 
toolbar.SetBackgroundColor(Android.Graphics.Color.ParseColor("#ebebeb"));

【讨论】:

  • 啊,谢谢你的帮助。 DrawerArrowStyle 周围的东西是否记录在某处?我在任何地方都找不到。
  • 当然没问题。关于 DrawerArrowStyle 是 ActionBarDrawerToggle 的一部分,您将在那里找到您需要的所有信息
猜你喜欢
  • 2016-04-07
  • 2016-03-08
  • 2016-01-25
  • 2019-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-09
相关资源
最近更新 更多