【问题标题】:Xamarin Forms custom theme not workingXamarin Forms 自定义主题不起作用
【发布时间】:2016-06-15 17:46:57
【问题描述】:

我有一个使用 Xamarin Forms 2.0 的 Android 应用程序。我制作了一个自定义主题来设置一些颜色。我创建了这些文件:

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>

然后我有一个 MainActivity 我将这个主题连接到应用程序:

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

但这不能正常工作:应用栏是白色而不是预期的蓝色,并且“android:windowBackground”也将我的 Entry 控件的文本和边框变为白色。

【问题讨论】:

  • 尝试使用 App.Xaml 编写样式
  • @hrishi 这不是我问的。
  • 您的项目中是否包含 Appcompatibility 库以及您在哪个平台上运行应用程序?

标签: android xamarin themes android-theme xamarin-forms


【解决方案1】:

尝试在清单中添加您的主题名称 (SmartbitLight),如下所示:

<?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>

路径:your_app/Droid/Properties/AndroidManifest.xml

【讨论】:

  • 实际上主题现在可以工作了,只是不是所有的颜色或显示(最值得注意的是工具栏是白色而不是蓝色)。您的代码 sn-p 只是设置主题的另一种方式(我通过设置 [Activity(Theme = "@style/SmartbitLight")]
  • @LeonCullens 嗨!哦,我明白了...但是您的文件styles.xaml,不需要用扩展名xml 重命名吗? :/ 你可以找到完整的信息。这里:motzcod.es/post/115523285992/… 和这里:developer.xamarin.com/guides/android/user_interface/… 另外,请确保将 >= v21 设置为编译目标。
  • @Johathan 绝对正确,它被命名为 .xml 但我打错了:) 我实际上找到了解决方案,我会添加我的答案。
【解决方案2】:

我设法让自定义主题与应用栏的所有颜色和所有内容一起使用。我创建了一个toolbar.axml 文件并将其注册到我的FormsAppCompatActivity 中,如下所述:https://blog.xamarin.com/material-design-for-your-xamarin-forms-android-apps/

【讨论】:

    猜你喜欢
    • 2020-05-08
    • 2023-03-24
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2020-10-22
    相关资源
    最近更新 更多