我遇到了同样的问题,我是这样解决的。
在清单中你应该添加:
<application android:label="yourApp" android:theme="@style/AppBaseTheme"></application>
你的基础活动应该扩展
:AppCompatActivity
你必须删除
这部分
// 使这个活动全屏
this.Window.AddFlags(WindowManagerFlags.Fullscreen);
也从你的所有活动中删除
主题 = "@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 将不起作用)在 Resources 下创建一个名为 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 要求您在活动布局中包含工具栏。
这是一个例子:
<?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 和布局工具栏.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 stackoverflow";
//..
}
只有在设置了 ContentView 之后才能设置工具栏。
这些步骤对我有用,并帮助我更新到 AppCompatv7 r22.2
据我所知,AppCompatv7 r22.1 有一些小错误。
从 9 月 29 日开始,还有一个新版本的 AppCompat v7 (r.23)