Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use multiple shapes in one Android drawable?

Tags:

What I'm trying to do is define a common background for use in a LinearLayout which frequents my application on many of its Activitys. This particular layout is a header that shows on the top of each activity.

What I'm trying to do is create a drawable that fills the linearlayout with a gradient and has a horizontal line below the gradient.

Does anyone know if this is possible or do I have to do this sort of thing only with nested layouts.

My attemptat the drawable xml is

 <selector 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:startColor="#AA000000" android:endColor="#AA333333"         android:angle="270" /> </shape> </item> <item> <shape android:shape="line">     <stroke android:width="3dp" android:color="#FFFFFFFF"             android:dashWidth="1dp" android:dashGap="2dp" />     <size android:height="5dp" /> </shape> </item> </selector> 
like image 765
David Brown Avatar asked Jan 19 '11 20:01

David Brown


People also ask

What is drawable shape android?

A ShapeDrawable takes a Shape object and manages its presence on the screen. If no Shape is given, then the ShapeDrawable will default to a RectShape . This object can be defined in an XML file with the <shape> element.

What is stroke in shape android?

Sometimes you want an outline around your shape and to do that you can use the stroke tag. You can specify the width and color of the outline using android:width and android:color. Since your shape is a rectangle, you can round rectangle's corners. You can do that inside of the corners tag.

What is Draw able folder in android?

A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon . There are several different types of drawables: Bitmap File.


2 Answers

Drawable accepts multiple shapes, (defined always in others files) if i understand your question maybe you can do a Layer drawable (this draws multiple underlying drawables on top of each other)

i write this here, so, i dont tested, but try this and read this fantastic documentation.

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

the android complete xml resources

cheers

like image 115
Franco Avatar answered Oct 31 '22 19:10

Franco


Use gravity to set your items in layer-list.

     for vertical orientation use:     android:gravity="top"     android:gravity="center"     android:gravity="bottom" 
     for horizontal orientation use:     android:gravity="left"     android:gravity="center"     android:gravity="right" 
<layer-list            xmlns:android="http://schemas.android.com/apk/res/android">           <item>                 <bitmap                    android:src="@drawable/top_img"                    android:gravity="top"/>           </item>           <item>                 <bitmap                    android:src="@drawable/center_img"                    android:gravity="center"/>           </item>           <item>                 <bitmap                    android:src="@drawable/bottom_img"                    android:gravity="bottom"/>           </item> </layer-list> 

Be sure that the parent element has enough space to hold all the items! Otherwise they will overlap each other

like image 29
Dani Avatar answered Oct 31 '22 18:10

Dani