简体   繁体   中英

Android make activity layout look the same as splash screen drawable

I am making a splash screen by changing the

<style name="Theme.Transparent" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/background_splash</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

property of a theme.

This drawable/background_splash looks like this

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
    <item android:drawable="@color/colorPrimary"/>
    <item>
        <bitmap
            android:src="@drawable/ic_logo_black"
            android:gravity="center"/>
    </item>
</layer-list>

For my launcher activity, it does some calculation in the back, and then conditionally navigate to other activities. This calculation requires server so it will stay on this activity for some time.

In order to make the experience better, I would like this activity to look the same as the splash screen.

So I basically did this for the layout.

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

</RelativeLayout>

However, with this setup. I can notice that the logo is shifted upwards by about 5-10 px once it goes from splash screen to the launcher activity.

My question is, what can I do so that this shift is completely gone? I can obviously hardcode some margins around. But i am not sure if that will for all devices sizes.

What is that 5-10px shift anyway? i basically used the exact image as the background in the layout.

I notice that if I don't set any background for my splashActivity layout, this issue is resolved. But I would still like a background because during my user work flow, user might be redirected to this screen. I don't want them to see an empty blank screen =/

It's always better and safe to tie an image with ImageView .

Both in Splash and Launcher Activity, just an ImageView inside RelativeLayout and set scaleType to centerCrop

As explained here

android:background is the background color (drawable to be precise) of a view component whereas android:windowBackground is the background color of the window (activity or dialog) in which your view resides. So this could be one of the reason behind 5px padding. (I am not sure about it).

On the other hand you can set the theme of the launcher activity dynamically by doing :

public void onCreate(Bundle savedInstanceState) {
    setTheme(android.R.style.Theme);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
}

just remember to use setTheme before super.onCreate

I somehow got away by not setting any background image for my SplashActivity.

And then it is transparent and sees the initial window background.

Thank you for your nice question

I found a way, change 'drawable/background_splash' to

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
     <item>
       <color android:color="@color/colorPrimary" />
     </item>
     <item  android:bottom="48dp">
         <bitmap
             android:src="@drawable/ic_logo_black"
             android:layout_width="wrap_content"
             android:tileMode="disabled"
             android:layout_height="wrap_content"
             android:scaleType="fitCenter"/>
     </item>
 </layer-list>

Seems bar height(48dp) in splash layout is not bound !

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