简体   繁体   English

使用不同背景颜色的相对布局的Android边距

[英]android margin for relative layout with different background colors

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. (设置了fill_parent)我需要在左右两侧留一个边距。 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" >

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>

Any kind of border , for any layout can be achieved by using Shape Drawable. 使用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. 在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. 请参阅此链接以获取详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM