Android Studio 可重用 xml 值

Android Studio reusable xml value

我有一个超级简单的问题,但我找不到答案。 我想创建一个变量,然后我可以将其用于多个元素。 strings.xml 中的类似内容:

<resources>
    <string name="textSize">20sp</string>
</resources>
...
<EditText android:textSize="@string/textSize" />

但这不起作用。 我能够在 themes.xml:

中通过以下方式完成我想要的
<style name="textSize">
    <item name="android:textSize">20sp</item>
</style>
...
<EditText android:theme="@style/textSize" />

但是单个值好像太复杂了,有没有更好的办法?

正如评论中所建议的,这看起来像 dimen 属性 而不是 string

就像 strings.xml 一样,您可以拥有另一个具有维度的文件(通常是 dimen.xml)。您的案例可能如下所示:

<resources>
    <dimen name="bigText">20sp</string>
</resources>

它还允许您针对不同的配置进行不同的设置(例如,此处的屏幕尺寸:)。

您可以在此处找到文档: https://developer.android.com/guide/topics/resources/more-resources?hl=en#Dimension