【问题标题】:Xamarin Forms set Android in LightXamarin Forms 为 Android 带来了光明
【发布时间】:2018-03-02 17:21:41
【问题描述】:

我什至用 Google 都找不到如何将 Android 项目设置为 Light Theme。

“虚线”按钮后面的工具栏一直是黑色的,我该如何让它变白?

我的意思是当你按下右上角的“虚线”按钮时弹出的导航。

<?xml version="1.0" encoding="utf-8" ?>
<resources>

  <color name="ListViewSelected">#ff9933</color>
  <color name="ListViewHighlighted">#E39696</color> 

  <style name="MainTheme" parent="MainTheme.Base">
    <item name="android:colorPressedHighlight">@color/ListViewSelected</item>
    <item name="android:colorLongPressedHighlight">@color/ListViewHighlighted</item>
    <item name="android:colorFocusedHighlight">@color/ListViewSelected</item>
    <item name="android:colorActivatedHighlight">@color/ListViewSelected</item>
    <item name="android:activatedBackgroundIndicator">@color/ListViewSelected</item>
  </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">#ff9933</item>
    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">#ff9933</item>
    <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
    <item name="colorAccent">#ff9933</item>
    <!-- You can also set colorControlNormal, colorControlActivated
         colorControlHighlight and colorSwitchThumbNormal. -->
    <item name="windowActionModeOverlay">true</item>

    <item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
  </style>

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

【问题讨论】:

    标签: c# xaml xamarin.forms styles themes


    【解决方案1】:

    我相信你最好写一点特定于平台的代码:

    安卓

    在您的 MainActivity.cs 上编写代码,在重写的 OnCreate 方法中的代码如下所示:

    protected override void OnCreate(Bundle bundle)
            {
                TabLayoutResource = Resource.Layout.Tabbar;
                ToolbarResource = Resource.Layout.Toolbar;
    
                base.OnCreate(bundle);
    
                global::Xamarin.Forms.Forms.Init(this, bundle);            
                LoadApplication(new App());
                Window.SetStatusBarColor(Android.Graphics.Color.Argb(255, 0, 0, 0)); //here
            }
    

    【讨论】:

    • 是的,我知道如何更改工具栏的颜色,但如果您按下“虚线”按钮,我说的是工具栏项的背景!
    【解决方案2】:

    您需要在 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 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>
    

    【讨论】:

    • 我有你发布的内容,但工具栏项列表的背景保持黑暗。
    • @user7849697,我为工具栏主题添加了 CustomActionBarPopupTheme。请参阅我的更新答案。并在主样式中配置它。
    • 我使用了您提供的代码,但它仍然是黑暗的。观看我上传的图片。
    • 请分享你的 style.xml。
    • 您没有在样式文件中添加 CustomActionBarPopupTheme。还添加 @style/CustomActionBarPopupTheme 看我的代码,你错过了一些行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多