2011-10-03 17 views
9

如何在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> 

然后我想覆盖startColor和ENDCOLOR(也许弯道半径过或任何其他财产)在每个绘制的XML文件。

我试着使用父母和样式,但他们都没有使用任何属性。例如:

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

然后被拉伸的样子:

<gradient style="@style/base_gradient" /> 

这没有奏效。我试图把类似上述在自己的XML文件,然后执行此操作为每个绘制:

<gradient parent="@drawable/base_gradient" /> 

这也不能工作。

有没有办法做到这一点?

回答

7

不幸的是我不认为这是可能的。我试图做同样的事情,并找不到解决方案。

我会建议将所有的值放在资源XML文件中。在我的情况下,我选择将我的尺寸放在dimens.xml,& integers.xml和colours.xml中的颜色(虽然它们可以合并成一个文件)

虽然我最终为每个颜色,至少如果我想调整颜色或填充等我只需要编辑colours.xml文件integers.xml或dimens.xml文件。

我的一个形状绘制的则是这样的:

<?xml version="1.0" encoding="UTF-8"?> 

<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" > 

    <corners 
     android:radius = "@dimen/radius_corner" /> 

    <!-- end colour at top, start colour at bottom --> 
    <gradient 
     android:angle="@integer/gradient_angle_default" 
     android:endColor="@color/white" 
     android:startColor="@color/pink_pale"/> 

    <padding 
     android:left = "@dimen/my_padding" 
     android:right = "@dimen/my_padding" 
     android:bottom = "@dimen/my_padding" 
     android:top = "@dimen/my_padding" /> 

</shape> 

资源文件链接:http://developer.android.com/guide/topics/resources/more-resources.html

希望这有助于。