Dunnow
Dunnow

Reputation: 414

various XML shapes as one drawable

I'm trying to set a background via a XML file that has to contain various shapes, I don't whant them to overlap (i've already played with that) but I want them to be one below the other.

I already have a layer-list defined in one xml, and now I want to have a shape under that one.

How do I accomplish that using only xml?

Thanks, I've been looking arround but all I find it's information about layer-list and not this particular situation. I'm sure there's a post about it arround here, but I can't seem to find it. Sorry in advance.

PD: Since I'm asking, any way to accomplish a blur effect on a shape?

Edit: Another way to ask the same question: I have a rectangle.xml and a circle.xml, how to I put one below the other for it to be used in a background.

Upvotes: 1

Views: 2485

Answers (1)

MH.
MH.

Reputation: 45493

There are two obvious options here:

  1. Split the layers out into separate drawables and assign them to different views in e.g. a vertically orientation LinearLayout.
  2. Specify an appropriate value for the android:top attribute to offset the second (and 3rd an 4th etc) drawable.

Example for 1:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/drawable1" />
    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/drawable2" />
</LinearLayout>

If your current LayerDrawable depicts some sort of state, you may also want to look into StateListDrawable or LevelListDrawable.

Example for 2:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/drawable1" />
    <item android:drawable="@drawable/drawable1" android:top="50dp" />
</layer-list>

The second option requires some a priori knowledge about the size of the first item. If you define the drawable using xml, you'll have to set the at appropriate offset value at design time.

Upvotes: 3

Related Questions