如何在不使用styles.axml的情况下修复android api> = Kitkat上的BarTextColor

问题描述 投票:0回答:1

我注意到Android> = KitKat,我无法更改工具栏项目上的默认文本颜色。我可以改变的是背景颜色。我在这里使用styles.axml尝试了所有的东西。我的工具栏背景颜色为白色,文本和菜单图标必须为蓝色。在KitKat上,一切都是白色的,包括文本。 (在我的styles.axml上,我没有设置任何颜色的白色。所以我猜是默认的。)以下解决方案适用于所有其他SDK - > App.xaml

 <Style TargetType="NavigationPage">
        <Setter Property="BarBackgroundColor" Value="ffffff"/>
        <Setter Property="BarTextColor" Value="#1c4d9e"/>
  </Style> 

- 我的工作原理 - 因为我无法使用样式.xaml实现颜色更改。 -NOTE-我只是作为一个极端的解决方案来检测SDK是否是KitKat,只是修改工具栏的背景颜色。因为文字是白色的。我的目标是更改工具栏上每个元素的颜色,而不管SDK

public void KitKat() 
      {
        var Check = DependencyService.Get<Interfaces.ISDK>();
        var a = Check.IsKitkat();
        if(a == true) 
        {
           new Setter {Property = NavigationPage.BarBackgroundColorProperty, Value = Color.DarkRed};
           new Setter {Property = NavigationPage.BarTextColorProperty, Value = Color.White};

         }

      }

工具栏示例

<ContentPage.ToolbarItems>
        <ToolbarItem  Text="Help" 
              Order="Primary"  Command="{Binding HelpCommand}" />
           <ToolbarItem  AutomationProperties.IsInAccessibleTree="true"  
          Text="Next" Order="Secondary"  Command="{Binding NextCommand}" />
       </ContentPage.ToolbarItems>
text xamarin.forms colors android-toolbar
1个回答
1
投票

首先,我通过以下操作更改了Help选项卡。

1.我在secondary_text中添加colors.xml项目,

<color name="secondary_text">#DC143C</color>

2.我在<item name="actionMenuTextColor">@color/secondary_text</item><style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">中加入style.xml

在Android 8.1和android 4.4中运行时有我的截图

Android 8.1

enter image description here

Android 4.4

enter image description here

然后,我通过以下操作更改了Next选项卡。

  1. 我在styles.xml创建了一个新的风格,有代码。

 <style name="my.Base" parent="Theme.AppCompat.Light.DarkActionBar">

    <item name="colorPrimary">#488FCE</item>
    <item name="colorPrimaryDark">#488FCE</item>

    <item name="android:windowNoTitle">false</item>
    <item name="android:windowActionBar">true</item>

    <item name="android:textColorPrimary">#ff0000</item>
    <item name="android:textColorPrimaryInverse">#ff0000</item>
    
    </style>
  1. 我在Toolbar.axml中使用这种风格

enter image description here

这是代码,请注意是app:popupTheme="@style/my.Base",而不是android:popupTheme

<?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:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

app:popupTheme="@style/my.Base"

/>

在Android 8.1和android 4.4中运行时有我的截图。

Android 8.1

enter image description here

Android 4.4

enter image description here

有我的演示,你可以参考它。 https://github.com/851265601/ToolbarItemDemo1

© www.soinside.com 2019 - 2024. All rights reserved.