如何设置相对背景颜色,例如半圆形?

3
我创建了一个相对布局,我想把背景颜色设置成半圆形在顶部。 我给出的样式如下,请帮助我。enter image description here

你目前尝试了什么? - Amy
2
我不知道如何到达那里。 - crickpatel0024
那你为什么在这里问问题呢? - Amy
3
使用自定义的Shape类和 ShapeDrawable 进行绘制。 - pskink
4个回答

15

我已经创建了下面的xml drawable,你可以使用它:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
 <item
    android:left="-150dp"
    android:right="-150dp"
    android:top="-200dp">
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
        <solid android:color="#C72C2F" />
    </shape>
</item>
</layer-list>

在此输入图片描述


6

enter image description here

只需将其复制并粘贴到DRAWABLE文件夹中的half_circle.xml文件中即可。

<?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
 <corners
    android:bottomLeftRadius="190dp"
    android:bottomRightRadius="190dp"
    android:topLeftRadius="0dp"
    android:topRightRadius="0dp" />
 <padding
    android:bottom="0dp"
    android:left="0dp"
    android:right="0dp"
    android:top="0dp" />
 <stroke
    android:width="0dp" />
 <gradient
    android:startColor="#2196f3"
    android:endColor= "#2196f3"
    android:angle="90" />
 </shape>

使用方法如下
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="100">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="48"
        android:background="@drawable/half_filled_circle">
     </RelativeLayout>
 </LinearLayout>

1
你可以创建一个9-patch图像,将其设置为背景。可拉伸的部分(即可拉伸的部分)将被设置在半圆形的下方。

只是RelativeLayout的背景颜色,没有图片。 - crickpatel0024

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接