Xamarin Forms custom theme not working

♀尐吖头ヾ 提交于 2019-12-22 19:25:10

问题


I have an Android app that uses Xamarin Forms 2.0. I made a custom theme to set some colors. I created these files:

Resources/values/styles.xml (AndroidResource)

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

  </style>

  <style name="SmartbitLight.Base" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/blueLight</item>
    <item name="colorPrimaryDark">@color/blueDark</item>
    <item name="colorAccent">@color/grey</item>
    <item name="android:windowBackground">@color/white</item>
    <item name="android:textColorPrimary">@color/white</item>
    <item name="android:statusBarColor">@color/blue</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
  </style>
</resources>

Resources/values-v21/styles.xml (AndroidResource)

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

  </style>
</resources>

Resources/values/colors.xml (AndroidResource)

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <color name="blueLight">#3cc1f1</color>
  <color name="blue">#33a4cd</color>
  <color name="blueDark">#33a4cd</color>
  <color name="grey">#7d7d7d</color>
  <color name="white">#ffffff</color>
</resources>

And then I have a MainActivity where I hook up this theme to the app:

[Activity(
    Label = "Smartbit", 
    Icon = "@drawable/icon", 
    MainLauncher = true, 
    ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
    Theme = "@style/SmartbitLight")]
public class MainActivity : FormsAppCompatActivity

This doesn't work correctly though: the app bar is white instead of the intended blue, and the 'android:windowBackground' turns the text and border of my Entry control white as well.


回答1:


Try adding the name of your theme (SmartbitLight) in the manifest, as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.test">
    <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="23" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:label="test" android:theme="@style/SmartbitLight"></application>
</manifest>

path: your_app / Droid / Properties / AndroidManifest.xml




回答2:


I managed to get the custom theme working with all the colors for the appbar and everything. I created a toolbar.axml file and registered it in my FormsAppCompatActivity as described here: https://blog.xamarin.com/material-design-for-your-xamarin-forms-android-apps/.



来源:https://stackoverflow.com/questions/35754590/xamarin-forms-custom-theme-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!