如何在 xml 中重新创建此背景? [英] How can I recreate this background in xml?

查看:32
本文介绍了如何在 xml 中重新创建此背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在 xml 中重新创建背景图像(可绘制).由于背景是一个简单的形状,最好创建为xml drawable.

I've been trying to recreate a background image in xml (as drawable). Since the background is a simple shape, it would be better if it is created as xml drawable.

是一个非常大的渐变圆,在左侧、顶部和右侧边框处呈方形.

The is a really large gradient circle that squares off at the left, top and right border.

我的尝试

<?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 个形状,一个矩形和一个椭圆形.

I've tried to create a layer-list. This layer list contains 2 shapes, a rectangle and an oval.

矩形有一个渐变和 20dp 的高度(用于测试).

The rectangle has a gradient and a height of 20dp (to test).

椭圆也有相同的渐变,顶部填充为 20dp.

The oval also has the same gradient, and has a top padding of 20dp.

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

This however, doesn't create a circular shape, but fills the whole shape with the gradient. Can someone help me?

推荐答案

试试这个

<?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>

输出

这篇关于如何在 xml 中重新创建此背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆