Reputation: 385
I have a relative layout that occupies full screen and has a white background. (fill_parent is set) I need a margin on the left and right side. The margin area should have a different background color. How do I set the background color for the margin area?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/c1_cnxlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:background="@color/purewhite" >
Upvotes: 2
Views: 3551
Reputation: 16317
Any kind of border , for any layout can be achieved by using Shape Drawable. In the case of Relative Layouts margin following can be done:--
Create a margin.xml file in drawable folder.I have added the comments in the code.
<?xml version="1.0" encoding="utf-8"?>
<!--By default the border shape is rectangle, can be changed using android:shape-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- This wil be the views background color, where this margin.xml is applied -->
<solid android:color="@color/view_bg"></solid>
<!-- Border color and its width is defined by stroke -->
<stroke
android:width="5dp"
android:color="@color/border_blue"></stroke>
<!-- The radius makes the corners rounded -->
<corners android:radius="10dp"></corners>
<!--represents the variation of color intensity in a direction represented by angle-->
<gradient
android:angle="45"
android:endColor="@color/gradient_end"
android:startColor="@color/gradient_start" />
</shape>
and add this in colors.xml file in values folder
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="view_bg">#b20e0f</color>
<color name="white">#ffffff</color>
<color name="btn_bg">#3e4a56</color>
<color name="border_blue">#1A237E</color>
<color name="gradient_start">#FFFF0000</color>
<color name="gradient_end">#80FF00FF</color>
</resources>
and finally in your relativeLayout tag add this:--
android:background="@drawable/margin"
Refer this link for detailed information.
Upvotes: 1
Reputation: 4124
Add another RelativeLayout
in it, set two different background colors
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/c1_cnxlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/color1" >
<RelativeLayout
android:id="@+id/c2_cnxlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:background="@color/color2" />
</RelativeLayout>
Upvotes: 4