开发者

Defining XML Parent Style of Gradient / Shape / Corners

How can I define an easily reusable base shape (or gradient, or corners) in XML?

I have a dozen or so drawable gradients that will be the same other than the start and end colors. I was hoping to define the identical stuff somewhere else and have an XML file for each different gradient that only defined the start and end colors. Is that possible?

This is the base:

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

Then I wanted to overwrite the startColor and endColor (and maybe the corners radius too or any o开发者_开发问答ther property) in each drawable's XML file.

I tried using both parent and styles, but neither of them use any of the properties. For example:

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

And then the drawable looks like:

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

That didn't work. I tried similarly putting the above in its own XML file and then doing this for each drawable:

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

That did not work either.

Is there a way to do this?


Unfortunately I don't think it is possible. I tried to do the same thing and could not find a solution.

What I would suggest is putting all your values in resource xml files. In my case I chose to put my dimensions in dimens.xml, & integers.xml and colours in colours.xml (though they could have been combined into one file)

While I ended up with a shape drawable file for each colour, at least if I want to tweak colours or padding etc I only need to edit the colours.xml file integers.xml or dimens.xml file.

One of my shape drawable then looked like this:

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

Resource file link: http://developer.android.com/guide/topics/resources/more-resources.html

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜