【问题标题】:android margin for relative layout with different background colors具有不同背景颜色的相对布局的android边距
【发布时间】:2014-09-06 10:31:42
【问题描述】:

我有一个占满全屏且背景为白色的相对布局。 (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" >

【问题讨论】:

    标签: android margin android-relativelayout


    【解决方案1】:

    在里面再添加一个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>
    

    【讨论】:

    • 这可行,但是,嵌套布局容器通常比使用单个布局容器使用更多的系统内存。
    • @Nicks 是的,这可能会导致一些性能问题,但这似乎是唯一的方法。而且我认为RelativeLayout 的一层不会造成太大的麻烦。你可能有更优雅的方式?
    • 我同意,对于小型项目和性能而言,它绝对更简单的解决方案也不会因为嵌套单个级别而受到影响。 :-)
    【解决方案2】:

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

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

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

    并将其添加到 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>
    

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

    android:background="@drawable/margin"
    

    有关详细信息,请参阅this link

    【讨论】: