Xamarin - AppCompat 不支持当前主题功能

Xamarin - AppCompat does not support the current theme features

我注意到这是一个非常常见的问题,但是 none 的解决方案对我有用。这是每个人似乎都引用的主线程:

但我试过了,但没有成功。我错过了什么吗??

这是我的清单

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto">
        <uses-sdk />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    </manifest>

这是我的styles.xaml:

    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">

    </style>

    <style name="AppTheme" parent="AppBaseTheme">
        <!-- Both of these are needed -->
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

这是我的基地activity:

    public abstract class ActivityBase : MvxCachingFragmentActivityCompat
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Make this activity fullscreen
            this.Window.AddFlags(WindowManagerFlags.Fullscreen);
        }

        .
        .
        .
    }

这是我的样本activity:

    [Activity(Label = "Add Child", Theme = "@style/AppTheme")]
    public class AddChildPage : ActivityBase, DatePickerDialog.IOnDateSetListener
    {
        .
        .
        .
    }

我基本上是直接在我正在测试的 activity 上设置主题。我没有在清单中设置主题,也没有在基础中设置主题 activity。

我是不是做错了什么?

更新:

按照这些步骤操作后,我很高兴地说错误消失了,但现在我得到了一个新的错误,这是我在引入 AppCompat 基础之前从未遇到过的 activity:

 java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.ViewGroup.getPaddingLeft()' on a null object reference

我会对此进行更多调查,如果找不到答案,我会将 Daniele 的答案标记为有效,因为它确实修复了我之前的错误。

我遇到了同样的问题,下面是我的解决方法。

您应该在清单中添加:

<application android:label="yourApp" android:theme="@style/AppBaseTheme"></application>

你的基地 activity 应该扩展

: AppCompatActivity

而且你必须删除 这部分

// Make this activity fullscreen this.Window.AddFlags(WindowManagerFlags.Fullscreen);

同时从您的所有活动中删除

Theme = "@style/AppTheme"

(这样他们才会这样)

[Activity(Label = "Add Child")]

现在,您的 styles.xaml 应该是:

    <resources>
  <style name="AppBaseTheme" parent="AppBaseTheme.Base">
  </style>
  <!-- Base theme applied no matter what API -->
  <style name="AppBaseTheme.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>
    <!-- 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">#40FF81</item>
    <!-- You can also set colorControlNormal, colorControlActivated
         colorControlHighlight and colorSwitchThumbNormal. -->
  </style>
</resources>

但是您还需要(这是必需的,否则 Appcompat 将不起作用)在资源下创建一个名为 values-v21 的新文件夹,该文件夹只能从 Android 5+ 的设备上看到,并且在里面您将有另一个styles.xaml 内容如下:

<resources>
  <style name="AppTheme" parent="AppTheme.Base">
    <item name="android:windowContentTransitions">true</item>
    <item name="android:windowAllowEnterTransitionOverlap">true</item>
    <item name="android:windowAllowReturnTransitionOverlap">true</item>
    <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
    <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
  </style>
</resources>

请注意,样式名称和父项必须与其他样式文件完全相同,但要与清单中声明的​​相同。

现在您需要直接从支持库中引用新的工具栏小部件。如果您还没有在 Resources/Layout/toolbar.axml 下创建一个新的 Android 布局文件,请创建它。该文件将如下所示:

<?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:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

通过工具栏设置,我们现在通过使用包含节点放置工具栏将其集成到您的布局中。 我知道,之前没有必要在每个布局中包含操作栏,但没有什么可做的:App Compat 要求您在 activity 布局中包含工具栏。 这是一个例子:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/main_content"
        android:layout_below="@id/toolbar">
    </LinearLayout>
</RelativeLayout>

你不应该改变的重要部分(或者根据你的 ID 和布局toolbar.xaml)是这个:

<include    android:id="@+id/toolbar"   layout="@layout/toolbar" />

我们快完成了: 在您的所有活动中添加:

using Toolbar = Android.Support.V7.Widget.Toolbar;

你的 OnCreate 方法应该像这样开始

protected override void OnCreate (Bundle bundle)
{
  base.OnCreate (bundle);
  // Set our view from the "main" layout resource
  SetContentView (Resource.Layout.main);
  var toolbar = FindViewById<Toolbar> (Resource.Id.toolbar);
  //Toolbar will now take on default actionbar characteristics
  SetSupportActionBar (toolbar);
  SupportActionBar.Title = "Hello from Whosebug";
  //..
}

只有设置了ContentView后才能设置toolbar。

这些步骤对我有用,帮助我更新到 AppCompatv7 r22.2 据我所知 AppCompatv7 r22.1 有一些小错误。 从 9 月 29 日开始,还有新版本的 AppCompat v7 (r.23)