【问题标题】:Android Studio reusable xml valueAndroid Studio 可重用的 xml 值
【发布时间】:2021-07-15 18:03:50
【问题描述】:

我有一个超级简单的问题,我找不到答案。 我想创建一个变量,然后可以将其用于多个元素。 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。所以&lt;dimen name="my_dimension"&gt;20sp&lt;/dimen&gt;
  • 请试试这个," "
  • @HenryTwist 这行得通,谢谢。

标签: android android-studio android-layout


【解决方案1】:

根据 cmets 的建议,这看起来像 dimen 属性,而不是 string

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

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

它还允许您针对不同的配置进行不同的设置(例如,此处的屏幕尺寸:How to define dimens.xml for every different screen size in android?)。

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

【讨论】:

    猜你喜欢
    • 2017-04-22
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多