[英]android margin for relative layout with different background colors
我有一个相对的布局,它占据了全屏并且具有白色背景。 (设置了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" >
在其中添加另一个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>
使用Shape Drawable可以实现任何边框,任何布局。 对于相对布局,可以执行以下操作:-
在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>
并将其添加到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"
请参阅此链接以获取详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.