aTa
aTa

Reputation: 641

How to set an image to RelativeLayout Background

I have a RelativeLayout with 2 Buttons inside.

One Button has layout_alignParentLeft="true" property and the other button has layout_alignParentRight="true"

I want set a background on my RelativeLayout that show an image in the middle. I have a play.9.png in my drawable and use following code:

<RelativeLayout
    android:id="@+id/relativeLayout12"
    android:layout_width="fill_parent"
    android:layout_height="70dp" 
    android:background="@drawable/play" // background line
 >

    <Button
        android:id="@+id/bAbout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="but1" />

    <Button
        android:id="@+id/bSync"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="but2" />
</RelativeLayout>

But when i use background line, my Buttons miss their position

See the image below enter image description here

But i want my RelativeLayout be like this:

enter image description here

What is wrong?

Upvotes: 2

Views: 13338

Answers (3)

aTa
aTa

Reputation: 641

finally i do it guys i used this code:

<RelativeLayout
    android:id="@+id/relativeLayout12"
    android:layout_width="fill_parent"
    android:layout_height="70dp" 
    android:background="@drawable/play" // background line
 >

    <Button
        android:id="@+id/bAbout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="but1" />

    <ImageView
         android:layout_width="fill_parent"
         android:layout_height="match_parent"
         android:scaleType="fitXY"
         android:src="@drawable/play" />

    <Button
        android:id="@+id/bSync"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="but2" />
</RelativeLayout>

and my result was like:

enter image description here

and this works. thanks everybody

Upvotes: 5

himanshu
himanshu

Reputation: 1980

I think you should remove

 android:layout_alignParentBottom="true"
 android:layout_alignParentTop="true"

from both the buttons and set the button height to 'fill_parent'.

Upvotes: 0

Ram kiran Pachigolla
Ram kiran Pachigolla

Reputation: 21181

<RelativeLayout
android:id="@+id/relativeLayout12"
android:layout_width="fill_parent"
android:layout_height="70dp" 
android:background="@drawable/play" // background line
>

<Button
    android:id="@+id/bAbout"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="but1" />

<Button
    android:id="@+id/bSync"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="but2" />
</RelativeLayout>

Upvotes: 0

Related Questions