XML颜色用于矩形的弯曲边缘之外的区域

时间:2023-02-09 17:59:59

I am making a rectangle for the background of a RelativeLayout and was wondering how I could make the area outside of the rounded edges of this rectangle to also be darker_grey. Currently, only the inside of this rectangle is darker_grey

我正在为RelativeLayout的背景制作一个矩形,并想知道如何使该矩形的圆角边缘之外的区域也变为darker_grey。目前,只有这个矩形的内部是darker_grey

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <solid android:color="@android:color/darker_gray" />
    <stroke android:width="1dip" android:color="@android:color/black"/>
    <corners android:radius="20dp" />
</shape>

2 个解决方案

#1


6  

You can do it by switching to a layer-list drawable, and layering your shape on top of a solid colour, like so:

您可以通过切换到图层列表drawable,并将图形分层为纯色来实现,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <color android:color="@android:color/darker_gray" />
    </item>
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
            <solid android:color="@android:color/darker_gray" />
            <stroke android:width="1dip" android:color="@android:color/black"/>
            <corners android:radius="20dp" />
        </shape>
    </item>
</layer-list>

#2


1  

Another way to implement background color to any view using shape:

使用形状为任何视图实现背景颜色的另一种方法:

<ImageView
        android:layout_width="200dp" 
        android:layout_height="200dp"
        android:background="@android:color/darker_gray"
        android:src="@drawable/shape"/>

Please decide view width and height based on your requirement.

请根据您的要求确定视图宽度和高度。

It will look like:

它看起来像:

XML颜色用于矩形的弯曲边缘之外的区域

#1


6  

You can do it by switching to a layer-list drawable, and layering your shape on top of a solid colour, like so:

您可以通过切换到图层列表drawable,并将图形分层为纯色来实现,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <color android:color="@android:color/darker_gray" />
    </item>
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
            <solid android:color="@android:color/darker_gray" />
            <stroke android:width="1dip" android:color="@android:color/black"/>
            <corners android:radius="20dp" />
        </shape>
    </item>
</layer-list>

#2


1  

Another way to implement background color to any view using shape:

使用形状为任何视图实现背景颜色的另一种方法:

<ImageView
        android:layout_width="200dp" 
        android:layout_height="200dp"
        android:background="@android:color/darker_gray"
        android:src="@drawable/shape"/>

Please decide view width and height based on your requirement.

请根据您的要求确定视图宽度和高度。

It will look like:

它看起来像:

XML颜色用于矩形的弯曲边缘之外的区域