android - 如何在 xml 中重新创建此背景?

标签 android android-layout android-xml android-drawable layer-list

我一直在尝试在 xml 中重新创建背景图像(可绘制)。由于背景是一个简单的形状,如果将其创建为 xml drawable 会更好。

[background image]是一个非常大的渐变圆圈,在左、上和右边界呈方形。


我尝试过的

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
           android:shape="rectangle">
            <size
               android:height="20dp" />
            <gradient
               android:type="linear"
               android:startColor="@color/gradientLeft"
               android:centerColor="@color/gradientMiddle"
               android:endColor="@color/gradientRight"
               android:angle="0" />
        </shape>
    </item>
    <item>
        <shape
           android:shape="oval">
            <padding
               android:top="20dp" />
            <gradient
               android:type="linear"
               android:startColor="@color/gradientLeft"
               android:centerColor="@color/gradientMiddle"
               android:endColor="@color/gradientRight"
               android:angle="0" />
        </shape>
    </item>
</layer-list>

我尝试创建一个层列表。该图层列表包含 2 个形状,一个矩形和一个椭圆形。

矩形具有渐变和 20dp 的高度(用于测试)。

椭圆形也有相同的渐变,顶部内边距为 20dp。


但是,这不会创建圆形,而是用渐变填充整个形状。有人可以帮助我吗?

最佳答案

试试这个

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" />
    </item>
    <item
        android:bottom="300dp"
        android:left="-100dp"
        android:right="-100dp"
        android:top="-80dp">
        <shape android:shape="oval">
            <size android:height="20dp" />
            <gradient
                android:angle="0"
                android:centerColor="@color/colorAccent"
                android:endColor="@color/red"
                android:startColor="@color/colorPrimary"
                android:type="linear" />
        </shape>
    </item>
</layer-list>

输出

enter image description here

关于android - 如何在 xml 中重新创建此背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49724329/

相关文章:

android - alignParentBottom 在模拟器中工作,但不在设备上工作

java - 在包 'paddingEnd' 中找不到属性 'android' 的资源标识符

Android Webview 底部有额外的空白

android - 如何从谷歌地图v2 android中的标记获取屏幕坐标

android - 在 Windows 7 中安装 Android USB 驱动程序

java - 将图像转换为草图然后卡通化?

android- 限制 edittext 只接受波斯语字符

android - 我想使布局边框如下图所示

android - Eclipse Android 布局设计器 - 添加用于预览的新屏幕尺寸

java - 将图像放在图像上