字符串 XML 常量未初始化 xml 文件中的值

String XML constant not initialising value in xml file

我有 37 个渐变文件。与其单独更新所有 37 个文件的 centreX 值,我认为最好将该值作为字符串常量存储在 strings.xml 文件中并在布局 xml 中引用它。这样,如果我更新 string.xml 中的值,所有 37 个单独的布局将自动更新。

这个问题,这并没有发生。请在下面查看我的两个代码片段:

 strings.xml
 <string name="gradient_size">70%</string>


 Layout file:
 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
    android:angle="90"
    android:centerColor="@color/white"
    android:endColor="@color/bored"
    android:startColor="@color/white"
    android:type="linear"
    android:centerX="@string/gradient_size"
    />
 </shape>

android:centerX="@string/gradient_size" 是字符串值的 70%。 但它不起作用。如果我删除常量并只输入 android:centerX="70%" 它就可以完美运行。但这意味着我必须单独更新所有 37 个文件。字符串值不起作用是否有一个简单的原因?

将常量声明为

<dimen name="gradient_size">75%</dimen>

而不是字符串使这项工作。希望对其他人有帮助。