<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="270"
android:endColor="#051d3e"
android:startColor="#062a53"
android:type="linear" />
</shape>
一、你当前代码中每个参数的含义
<shape
android:shape="rectangle">
android:shape="rectangle"
- 表示这是一个矩形 Shape
- 可用于
background/src/foreground
<gradient
android:angle="45"
android:startColor="#062a53"
android:endColor="#051d3e"
android:type="linear"
android:useLevel="true" />
1️⃣ android:type="linear"
-
渐变类型
-
可选值:
linear(线性,最常用)radial(径向)sweep(扫描)
👉 你这里是 线性渐变
2️⃣ android:startColor
- 渐变起点颜色
- 出现在“渐变方向的起点”
3️⃣ android:endColor
- 渐变终点颜色
- 出现在“渐变方向的终点”
4️⃣ android:angle="45"(重点)
定义渐变方向(角度)
⚠️ Android 的角度规则不是数学坐标系
官方规则(必须记住)👇
| angle | 渐变方向 |
|---|---|
0 | 左 → 右 |
90 | 下 → 上 |
180 | 右 → 左 |
270 | 上 → 下 |
45 | 左下 → 右上 |
135 | 右下 → 左上 |
225 | 右上 → 左下 |
315 | 左上 → 右下 |
5️⃣ android:useLevel="true"
⚠️ 这是一个高级属性
- 仅在
LevelListDrawable中才有意义 - 表示颜色随
level (0–10000)变化 - 作为普通背景,应该设为
false或直接删除
👉 你现在这个场景:不需要它
二、你现在的效果为什么不符合预期?
你当前是:
android:angle="45"
等价于:
左下 → 右上
但你期望的是:
左上浅色 → 右下深色
所以方向 错了
三、正确实现:左上浅色 → 右下深色 ✅
✅ 正确 angle:315
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:type="linear"
android:angle="315"
android:startColor="#062a53" <!-- 左上:浅色 -->
android:endColor="#051d3e" <!-- 右下:深色 -->
android:useLevel="false" />
</shape>
如果不写 useLevel(推荐)
<gradient
android:type="linear"
android:angle="315"
android:startColor="#062a53"
android:endColor="#051d3e" />
四、快速自检口诀(强烈建议记住)
想象一条箭头:
startColor → endColor
你要的是:
左上 → 右下
对应:
angle = 315
五、常见错误总结(你已经踩到一个)
| 错误 | 后果 |
|---|---|
| angle 用 45 | 方向完全反 |
| useLevel=true | 渐变可能异常 |
| 颜色写反 | 看起来“怪” |
| 不理解 angle | 调半天调不对 |
六、进阶建议(可选)
如果你后面还要:
- 深色模式
- 动态主题
- 不同尺寸渐变比例
可以升级为:
MaterialShapeDrawable- 或
Compose / Shader

434

被折叠的 条评论
为什么被折叠?



