You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android如何创建带渐变效果的线条Drawable?

实现Android渐变线条的正确方案

你的代码无法显示渐变的原因是Android的shape="line"的<stroke>标签不支持<gradient>属性,渐变无法作用在线条描边上,因此只会显示纯色线条;同时多余的rotate标签也没必要保留。

改用shape="rectangle"模拟渐变线条,通过控制矩形的宽高来实现线条效果,这样渐变就能正常生效:

垂直渐变线条(对应你预期的效果)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 宽度设为线条粗细,高度设为需要的长度 -->
    <size
        android:width="1dp"
        android:height="match_parent" />
    <gradient
        android:startColor="#FFEE8D85"
        android:endColor="#000000"
        android:type="linear" />
</shape>

水平渐变线条

如果需要水平方向的渐变线条,只需交换宽高设置:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size
        android:width="match_parent"
        android:height="1dp" />
    <gradient
        android:startColor="#FFEE8D85"
        android:endColor="#000000"
        android:type="linear" />
</shape>

这种方式利用矩形的渐变效果模拟线条,既可以正常显示渐变,也能灵活控制线条的长度和粗细,完全满足你的需求。

内容的提问来源于stack exchange,提问作者Raunak Seth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 05:05:29