Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android边距,用于不同背景颜色的相对布局_Android_Margin_Android Relativelayout - Fatal编程技术网

android边距,用于不同背景颜色的相对布局

android边距,用于不同背景颜色的相对布局,android,margin,android-relativelayout,Android,Margin,Android Relativelayout,我有一个相对的布局,占据了整个屏幕,有一个白色的背景。(已设置填充父项) 我需要在左右两边留一个边距。边距区域应具有不同的背景色。 如何设置边距区域的背景色 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/c1_cnxlayout" android:layout_width="fill_parent" android:layout_

我有一个相对的布局,占据了整个屏幕,有一个白色的背景。(已设置填充父项) 我需要在左右两边留一个边距。边距区域应具有不同的背景色。 如何设置边距区域的背景色

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

在其中添加另一个
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>

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

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

有关详细信息,请参阅。

但是,嵌套布局容器通常比使用单个布局容器使用更多的系统内存。@Nicks是的,这可能会导致一些性能问题,但这似乎是唯一的方法。而且我不认为再多加一层
RelativeLayout
会带来很多麻烦。您可能有一种更优雅的方法?我同意,对于小型项目来说,它无疑是一种更简单的解决方案,而且在嵌套单个级别时,性能也不会受到影响。:-)
<?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>
android:background="@drawable/margin"