Skip to content Skip to sidebar Skip to footer

How To Position A Custom Shape Inside A View?

I've defined a custom shape in my res/drawable folder:

Solution 1:

I am not sure there is a way to do position shape inside a view. However as a workaround I would consider something like this.

<RelativeLayout >
    <OldView with shape as background now without any background
        android:layout_alignParentTop="true" android:layout_alignParentBottom="true"/>
    <View withthis shape as background 
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

This will supposedly give you what you want to achieve.

Solution 2:

Try this , this may be useful,

http://thinkandroid.wordpress.com/2010/01/14/how-to-position-views-properly-in-layouts/

Set your custom view as background of layout or view. or if not useful then provide your layout xml file code here so i can help you more easily.

Solution 3:

The following receives the margin layout params of the shape so that you can change the views' margins, positioning it anyway you want:

MarginLayoutParams params = (MarginLayoutParams)shape.getLayoutParams();
// Sets the margins of the shape, making it move to a specific location on the screenparams.setMargins(int left, int top, int right, int bottom);

Solution 4:

I had a similar problem, but I wanted to have a background for my views, a rectangle, but only left and bottom sides of the rectangle to be shown, and no top or right ones. In order to achieve this, I have used this xml:

<?xml version="1.0" encoding="utf-8"?>

<itemandroid:bottom="2dp"android:left="2dp"android:right="2dp"android:top="2dp"><shapeandroid:shape="rectangle" ><strokeandroid:width="1px"android:color="#FFFF00" /></shape></item><itemandroid:bottom="3dp"android:left="3dp"android:right="1dp"android:top="1dp"><shapeandroid:shape="rectangle" ><strokeandroid:width="1px"android:color="#000000" /></shape></item>

Note: There are two rectangles, one drawn on top of the other, the second one drawn on top of the first, but a bit shifted, so that 1dp of the first one to be shown on screen on the left and bottom. Also, you must note that the color of the second one must be chosen to be the one who hides. In my case it was the black color.

The result is (You may observe yellow lines only on the left and bottom): Result example

Solution 5:

I know gravity works on other kinds of drawables 

android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
                          "fill_vertical" | "center_horizontal" | "fill_horizontal" |
                          "center" | "fill" | "clip_vertical" | "clip_horizontal"]

Post a Comment for "How To Position A Custom Shape Inside A View?"