【发布时间】:2017-11-08 08:53:54
【问题描述】:
我正在努力在 Xamarin.Android 的旧 Android 版本 (API 16) 上使用(基于 XML 的)矢量绘图创建启动画面。我已经阅读了很多类似的问题,但无论我尝试什么,我都无法让它发挥作用。
我目前有以下,在最近的 Android 版本(例如 API 25)上运行良好:
可绘制/itlogotext.xml:
<vector xmlns:android="http://schemas.android.com/apk/res/android" [...]>
[...]
</vector>
drawable/splash_screen.xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="#3498DB" />
</item>
<item
android:drawable="@drawable/itlogotext"
android:gravity="center" />
</layer-list>
values/styles.xml:
<resources>
[...]
<style name="MyTheme.Splash" parent ="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">@drawable/splash_screen</item>
<item name="colorPrimaryDark">#1B6698</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
SplashActivity.cs:
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Support.V7.App;
using JetBrains.Annotations;
[Activity(Theme = "@style/MyTheme.Splash", MainLauncher = true, NoHistory = true,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
ScreenOrientation = ScreenOrientation.Portrait)]
public sealed class SplashActivity : AppCompatActivity
{
protected override void OnResume()
{
base.OnResume();
this.StartActivity(new Intent(Application.Context, typeof(MainActivity)));
}
}
我有最新的支持库(26.1.0.1,包括 Xamarin.Android.Support.v4 和 Xamarin.Android.Support.v7.AppCompat,后者依赖于 Xamarin.Android.Support.Vector.Drawable)。
不幸的是,当我尝试在旧的 Android 版本(例如 API 16)上运行该应用程序时,我只是得到一个异常说 android.content.res.Resources$NotFoundException: File res/drawable/splash_screen.xml from drawable resource ID #0x7f020125,这条线在跟踪的底部:Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: invalid drawable tag vector。使用 splash_screen.xml 中的可绘制对象删除 <item> 可修复崩溃,但当然启动画面中不再有徽标。
我也尝试在构造函数、静态构造函数和OnCreate 中添加AppCompatDelegate.CompatVectorFromResourcesEnabled = true,但无论我把它放在哪里,结果都是启动屏幕根本不显示(即Android 应用程序抽屉直到显示应用程序的主屏幕)。
如何让初始屏幕上的 XML 矢量可绘制对象在早期的 Android 版本上工作?
【问题讨论】:
标签: c# xamarin.android android-drawable