相关疑难解决方法(0)

定义渐变/形状/角落的XML父样式

如何在XML中定义易于重复使用的基本形状(或渐变或角落)?

我有十几个可绘制的渐变,除了开始和结束颜色之外都是相同的.我希望在其他地方定义相同的东西,并为每个不同的渐变设置一个XML文件,只定义开始和结束颜色.那可能吗?

这是基础:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <gradient xmlns:android="http://schemas.android.com/apk/res/android"
        android:type="linear" 
        android:angle="270" 
    android:startColor="#e1ffffff"
    android:endColor="#e100ff00">

    <stroke android:width="1dp" 
            android:color="#ff000000" />

    <corners android:radius="4dp" />
</gradient>
</shape>
Run Code Online (Sandbox Code Playgroud)

然后我想在每个drawable的XML文件中覆盖startColor和endColor(也许是角半径或任何其他属性).

我尝试使用父级和样式,但它们都不使用任何属性.例如:

<style name="base_gradient">
    <item name="android:angle">270</item>
    <item name="android:type">linear</item>
    <item name="android:startColor">#e1ffffff</item>
    <item name="android:endColor">#e100ff00</item>
</style>
Run Code Online (Sandbox Code Playgroud)

然后drawable看起来像:

<gradient style="@style/base_gradient" />
Run Code Online (Sandbox Code Playgroud)

那没用.我尝试类似地将上面的内容放在它自己的XML文件中,然后为每个drawable执行此操作:

<gradient parent="@drawable/base_gradient" />
Run Code Online (Sandbox Code Playgroud)

那也行不通.

有没有办法做到这一点?

xml android gradient shape drawable

9
推荐指数
1
解决办法
5835
查看次数

标签 统计

android ×1

drawable ×1

gradient ×1

shape ×1

xml ×1