本文介绍了定义梯度/形状/角落XML父风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何定义一个容易的可重用的基础形状(或渐变,或角)的XML?

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

我有十几绘制渐变,这将是相同的以外的开始和结束的颜色。我希望以限定相同的东西别的地方,对每个不同的梯度,只有定义的开始和结束的颜色的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?

这是基础:

<?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文件。

Then I wanted to overwrite the startColor and endColor (and maybe the corners radius too or any other 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>

和再绘制如下:

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

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

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

这也不能工作。

有没有办法做到这一点?

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.

我的建议是把所有的值在资源的XML文件。在我来说,我选择把我的尺寸dimens.xml,和放大器; integers.xml和色彩colours.xml(尽管它们可以被合并成一个文件)

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)

当我结束了一个形状绘制文件,每种颜色,至少如果我想调整的颜色或填充等我只需要编辑colours.xml文件integers.xml或dimens.xml文件。

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>

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

希望这有助于。

这篇关于定义梯度/形状/角落XML父风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 03:48