简体   繁体   中英

Android: Center splash screen image

I have an app with a splash screen and the splash screen looks good on small devices but looks screwed up in a big tablet(emulator). so I changed the background to wrap_content . but it looks aligned to the side of the screen, Can some one tell me a way to center background images? here is my splash.xml-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/splash">
</LinearLayout>

I am using a background image for my activity. First create a theme in your styles.xml:

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/background_splash</item>
</style>

Then create a new drawable with the name background_splash.xml

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

Then set your activity to use this theme in your AndroidManifest.xml:

<activity android:name="SplashActivity" android:theme="@style/SplashTheme" >

This worked for me.

You don't actually need neither LinearLayout nor RelativeLayout here, use a more lightweight FrameLayout instead.

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:src="@drawable/splash"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</FrameLayout>

Slight change in code as it requires adding a second element for best fit

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#2A7800"<---- optional background color to fit the image. to make it blend in
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"<--- centers
        android:adjustViewBounds="true"
        android:scaleType="fitXY"<--- makes sure the image fits into the layout my matching the screen size.

        android:src="@drawable/splash" />
</LinearLayout>

Adding an ImageView is best practice as it is better performance wise than using android:background and setting an image. I discovered this with a ScrollView and the master parent had android:background set to an image. It slowed down the ScrollView extremly.

Additionally, there is no need for a specific layout with this method. You can use RelativeLayout, FrameLayout, LinearLayout - anything that works. The exception might be ListView, GridView

You can use relative layout, try the below code..

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:src="@mipmap/ic_launcher" />
</RelativeLayout>

Please try out the following

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/splash">
</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM