8

I need to set size for item in layer-list. For using as windowBackground in launch activity. I wrote this XML file and Android Studio displays it correctly. But in application the logo always full screen. How to make it?

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >

        <gradient
            android:endColor="#000004"
            android:gradientRadius="1000"
            android:startColor="#1f3371"
            android:type="radial" >
        </gradient>
    </shape>
</item>
<item>
    <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/texture_5"
        android:tileMode="repeat" />
</item>
<item android:drawable="@drawable/logo"
    android:height="100dp"
    android:width="77dp"
    android:gravity="center"
    >

</item>
</layer-list>
Tolyas
  • 442
  • 1
  • 6
  • 18

4 Answers4

11

I found solution, but I expected better way. Here is final code:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >

        <gradient
            android:endColor="#000004"
            android:gradientRadius="1000"
            android:startColor="#1f3371"
            android:type="radial" >
        </gradient>
    </shape>
</item>
<item>
    <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/texture_5"
        android:tileMode="repeat" />
</item>
<item android:drawable="@drawable/logo"
    android:bottom="214dp"
    android:top="214dp"
    android:left="100dp"
    android:right="100dp"
    android:gravity="center"
    android:tileMode="repeat"
    >
</item>
</layer-list>

At least it works at xxhdpi and xhdpi without distortion.

Tolyas
  • 442
  • 1
  • 6
  • 18
8

Starting from API 23, it is possible to set width and height right within the "item" tag:

<item android:drawable="@drawable/logo" android:width="..." android:height="..." />
android developer
  • 114,585
  • 152
  • 739
  • 1,270
1

You can do the followin

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/blanco"/>
    <item
        android:width="200dp"
        android:height="200dp"
        android:gravity="center">
        <bitmap
            android:gravity="fill"
            android:src="@drawable/your_image"
            android:mipMap="true"/>
    </item>
</layer-list>

Just change width and height

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
0

I had the same problem and found the solution here. I had to use my vector drawable as the source to a bitmap object:

<item>
    <bitmap
        android:gravity="center"
        android:src="@drawable/ic_logo_splash"/>
</item>
bernardo.g
  • 826
  • 1
  • 12
  • 27