相对布局的android边距与不同的背景颜色

时间:2021-02-11 21:04:56

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?

我有一个相对布局占据全屏幕并具有白色背景。 (fill_parent已设置)我需要左侧和右侧的边距。边距区域应具有不同的背景颜色。如何设置边距区域的背景颜色?

<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" >

2 个解决方案

#1


4  

Add another RelativeLayout in it, set two different background colors

在其中添加另一个RelativeLayout,设置两种不同的背景颜色

<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>

#2


1  

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:--

任何类型的边框,对于任何布局都可以通过使用Shape Drawable来实现。在相对布局的情况下,可以进行以下操作: -

Create a margin.xml file in drawable folder.I have added the comments in the code.

在drawable文件夹中创建一个margin.xml文件。我在代码中添加了注释。

<?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

并将其添加到values文件夹中的colors.xml文件中

<?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:--

最后在你的relativeLayout标签中加上: -

android:background="@drawable/margin"

Refer this link for detailed information.

有关详细信息,请参阅此链接。

#1


4  

Add another RelativeLayout in it, set two different background colors

在其中添加另一个RelativeLayout,设置两种不同的背景颜色

<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>

#2


1  

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:--

任何类型的边框,对于任何布局都可以通过使用Shape Drawable来实现。在相对布局的情况下,可以进行以下操作: -

Create a margin.xml file in drawable folder.I have added the comments in the code.

在drawable文件夹中创建一个margin.xml文件。我在代码中添加了注释。

<?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

并将其添加到values文件夹中的colors.xml文件中

<?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:--

最后在你的relativeLayout标签中加上: -

android:background="@drawable/margin"

Refer this link for detailed information.

有关详细信息,请参阅此链接。