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



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

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

最新更新