更改操作栏颜色

时间:2017-09-25 11:37:56

标签: c# xamarin xamarin.forms

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

当我将操作栏的默认颜色更改为白色(背景色)时,我需要系统自动更改标题并将箭头颜色返回黑色(它们是白色且不可见)。正如我上面所说,在iOS应用程序中只有这个代码,系统已经自动进行此切换,但对于Android我不知道如何做同样的效果。

请遵循以下代码:

    IIOBuilder ioBldr = JRecordInterface1.SCHEMA_XML .newIOBuilder(copybookName);


        try {
            AbstractLine saleRecord = ioBldr.newLine();
            AbstractLineWriter writer  = ioBldr.newWriter(salesFileOut);

            saleRecord.getFieldValue("KEYCODE-NO").set(1331);
            saleRecord.getFieldValue("STORE-NO").set(1);
            saleRecord.getFieldValue("DATE").set(80921);
            saleRecord.getFieldValue("DEPT-NO").set(100);
            saleRecord.getFieldValue("QTY-SOLD").set(7);
            saleRecord.getFieldValue("SALE-PRICE").set(7.00);
            writer.write(saleRecord);

            saleRecord.getFieldValue("STORE-NO").set(11);
            writer.write(saleRecord);

            saleRecord.getFieldValue("STORE-NO").set(121);
            writer.write(saleRecord);

            System.out.println(" " + saleRecord.getFieldValue("SALE-PRICE").asString());
            writer.close();
        } catch (Exception e) {

1 个答案:

答案 0 :(得分:1)

您需要在droid / resource / styles.xml中创建自定义主题。并且还需要在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>

了解更多

enter image description here