不明白 sintaxis 使用 ?在文中出现
Don't understand sintaxis using ? in textAppearance
我一直在学习 textAppearance 属性,但我不明白使用 ? 的 sintaxis。
当我们使用这个 sintaxis 时,我完全理解:
android:textAppearance="@android:style/TextAppearance.Material.Headline"
但是我不明白我们什么时候使用这个语法轴:
?android:attr/textAppearanceLarge
这两种定义textAppearance的方式有什么区别?
使用@android/style是可以理解的,因为我们引用了样式资源,但是为什么还有这种引用样式的方式呢?
您可以查看 official doc:
A style attribute resource allows you to reference the value of an attribute in the currently-applied theme. Referencing a style attribute allows you to customize the look of UI elements by styling them to match standard variations supplied by the current theme, instead of supplying a hard-coded value. Referencing a style attribute essentially says, "use the style that is defined by this attribute, in the current theme."
To reference a style attribute, the name syntax is almost identical to the normal resource format, but instead of the at-symbol (@
), use a question-mark (?
), and the resource type portion is optional.
您可以使用样式,例如:
android:textAppearance="@style/TextAppearance.MaterialComponents.Subtitle1"
您使用的是这种方式:
<style name="TextAppearance.MaterialComponents.Subtitle1" parent=".....">
...
</style>
您可以使用 应用程序主题中定义的 属性:
android:textAppearance="?attr/textAppearanceSubtitle1"
在您的应用主题中:
<style name="AppTheme" parent="Theme.MaterialComponents.*">
<item name="textAppearanceSubtitle1">@style/TextAppearance.MaterialComponents.Subtitle1</item>
</style>
其他资源使用相同的行为。例如颜色:
<style name="AppTheme" parent="Theme.MaterialComponents.*">
<item name="colorPrimary">@color/....</item>
</style>
在视图中您可以使用:
app:backgroundTint="?attr/colorPrimary"
我一直在学习 textAppearance 属性,但我不明白使用 ? 的 sintaxis。
当我们使用这个 sintaxis 时,我完全理解:
android:textAppearance="@android:style/TextAppearance.Material.Headline"
但是我不明白我们什么时候使用这个语法轴:
?android:attr/textAppearanceLarge
这两种定义textAppearance的方式有什么区别?
使用@android/style是可以理解的,因为我们引用了样式资源,但是为什么还有这种引用样式的方式呢?
您可以查看 official doc:
A style attribute resource allows you to reference the value of an attribute in the currently-applied theme. Referencing a style attribute allows you to customize the look of UI elements by styling them to match standard variations supplied by the current theme, instead of supplying a hard-coded value. Referencing a style attribute essentially says, "use the style that is defined by this attribute, in the current theme."
To reference a style attribute, the name syntax is almost identical to the normal resource format, but instead of the at-symbol (
@
), use a question-mark (?
), and the resource type portion is optional.
您可以使用样式,例如:
android:textAppearance="@style/TextAppearance.MaterialComponents.Subtitle1"
您使用的是这种方式:
<style name="TextAppearance.MaterialComponents.Subtitle1" parent=".....">
...
</style>
您可以使用 应用程序主题中定义的 属性:
android:textAppearance="?attr/textAppearanceSubtitle1"
在您的应用主题中:
<style name="AppTheme" parent="Theme.MaterialComponents.*">
<item name="textAppearanceSubtitle1">@style/TextAppearance.MaterialComponents.Subtitle1</item>
</style>
其他资源使用相同的行为。例如颜色:
<style name="AppTheme" parent="Theme.MaterialComponents.*">
<item name="colorPrimary">@color/....</item>
</style>
在视图中您可以使用:
app:backgroundTint="?attr/colorPrimary"