Android splash screen: how to make it centered?

2019-09-17 15:42发布

问题:

I use a theme-based solution as it described here, for example, to show a splash screen in my application.
Here is my background_launch.xml:

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/launch_image"
android:tileMode="disabled" />

When I have launch_image as png everything is OK. But the app crashes if I use @drawable/layerlist instead of @drawable/launch_image.
layerlist.xml is:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/launch_image"
            android:tileMode="disabled" >
        </bitmap>
    </item>
</layer-list>

The app crashed with the following log (several lines are skipped):

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.glu.pathlight/com.glu.pathlight.JSFinalNativeActivity}: android.content.res.Resources$NotFoundException: File res/drawable/background_launch.xml from drawable resource ID #0x7f020000
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
        ...
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/background_launch.xml from drawable resource ID #0x7f020000
    at android.content.res.Resources.loadDrawable(Resources.java:2096)
    at android.content.res.Resources.getDrawable(Resources.java:700)
    ... 11 more
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #18: <bitmap> requires a valid src attribute
    at android.graphics.drawable.BitmapDrawable.inflate(BitmapDrawable.java:571)
    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:937)
    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
    at android.content.res.Resources.loadDrawable(Resources.java:2092)
    ... 22 more

What is wrong in my resources?

回答1:

Put this:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
    <bitmap
        android:gravity="center"
        android:src="@drawable/launch_image"
        android:tileMode="disabled" >
    </bitmap>
</item>
</layer-list>

in your background_launch.xml instead of trying to reference a state list in android:src for <bitmap\>

A state list is not a bitmap -- that's what the error's telling you.



回答2:

Check if you have changed android:src="@drawable/launch_image" to android:src="@drawable/layerlist".