Android: how to align 2 images on a splash screen
Asked Answered
A

3

7

I would like to use a splah screen containing 2 images:

  • the main image must be fully center
  • the secondary image must be center between the bottom and the main image

The expected render is something like this: expected splash

But I don't see how to get this, and my second image is bottom aligned: actual splash

The XML of my splash is:

 <?xml version="1.0" encoding="utf-8" ?>
 <layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="rectangle">
                <solid android:color="@color/ComplementColor" />
                <padding 
                    android:left="0dip" 
                    android:top="0dip"
                    android:right="0dip" 
                    android:bottom="0dip" />
            </shape>
        </item>
        <item>
            <bitmap android:src="@drawable/main_logo"
               android:gravity="center" />
        </item>
        <item>
            <bitmap android:src="@drawable/secondary_logo"
               android:gravity="bottom" />
        </item>
</layer-list>
Alewife answered 15/9, 2017 at 18:54 Comment(0)
A
12

I've finally choose another solution close to that suggested by @Leonardo Cavazzani.

I've added a margin to the bottom image like this:

<?xml version="1.0" encoding="utf-8" ?>
  <layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
      <shape android:shape="rectangle">
        <solid android:color="@color/ComplementColor" />
          <padding 
            android:left="0dip" 
            android:top="0dip"
            android:right="0dip" 
            android:bottom="0dip" />
      </shape>
    </item>
    <item>
      <bitmap android:src="@drawable/main_logo"
              android:gravity="center" />
    </item>
    <item
      android:bottom="40dp">
      <bitmap android:src="@drawable/secondary_logo"
              android:gravity="bottom" />
     </item>
  </layer-list>
Alewife answered 22/9, 2017 at 18:17 Comment(1)
but its not a good solution as it gives issue with different device resolutions.Nonconformist
G
1

Set a marginBottom for your bitmap

<bitmap android:src="@drawable/secondary_logo"
                android:gravity="bottom"
                android:layout_marginBottom="100dp" />
Greyson answered 15/9, 2017 at 19:25 Comment(1)
Thanks for your reply, but this doesn't work. In the XML editor, I get the warning "attribute is not declared", and when I launch the app, it doesn't change anything.Alewife
T
0

You can achieve it trough the LayoutXML instead of the Drawable.XML

A simple LayoutManager where the first image is centered and the second alignedToTheBottom of the first plus topMargin solve your problem.

Telepathist answered 15/9, 2017 at 21:21 Comment(2)
I don't understand what you're talking about... Do I need to replace layer-list by LinearLayout? Or do you have a sample?Alewife
Yes, thats what i meantTelepathist

© 2022 - 2024 — McMap. All rights reserved.