android - 如何创建半椭圆形(弯曲一条线)

标签 android xml drawable shapes

我正在尝试为我的 NavigationView 页脚创建自定义形状作为背景。但它不是那么干净。这就是我所做的:

enter image description here

这就是我需要的:

enter image description here

代码:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:gravity="bottom">
    <shape android:shape="rectangle">
        <solid android:color="@color/darkerGray" />
    </shape>
</item>
<item

    android:gravity="bottom|center_horizontal"
    android:top="50dp">
    <!--android:top="-40dp"-->

    <shape android:shape="oval">
        <solid android:color="#ffffffff" />
    </shape>
</item>
<item

    android:bottom="30dp"
    android:gravity="bottom">
    <shape android:shape="rectangle">
        <solid android:color="#ffffffff" />
    </shape>
</item>

你能帮帮我吗?

最佳答案

#。更新您的自定义 drawable XML,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Container GRAY rectangle -->
    <item>
        <shape android:shape="rectangle">

            <size
                android:width="250dp"
                android:height="100dp" />

            <solid
                android:color="@android:color/darker_gray" />
        </shape>
    </item>

    <!-- Top WHITE oval shape -->
    <item
        android:left="-25dp"
        android:right="-25dp"
        android:top="-50dp"
        android:bottom="50dp">

        <shape android:shape="oval">

            <solid
                android:color="@android:color/white" />
        </shape>
    </item>
</layer-list>

预览:

enter image description here

#。在您的布局中使用此自定义 drawable,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:background="@android:color/black"
    android:padding="16dp">

    <!-- Custom Footer -->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/custom_shape">

    </LinearLayout>
</LinearLayout>

输出:

enter image description here

希望对你有帮助~

关于android - 如何创建半椭圆形(弯曲一条线),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45083229/

相关文章:

android - WebView JavaScript 桥文档

android - 在 xml 布局中使用 android MediaController

C# 绘图库

java - 添加两个drawable到EditText中

android - 图层列表中可绘制的图像按钮?

android - 使用内置的 Android 图像查看器打开存储在 res/drawable 中的图片

java - 简单的 smack jabber 程序需要很长时间才能在 android 上连接

Android 更改 ImageView/Bitmap 的颜色

android - 适用于 iOS/Android 的可重排 epub SDK

javascript - 如何使用 javascript 查询 XML 文件中的数据?