簡體   English   中英

使用不同背景顏色的相對布局的Android邊距

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM