如何从布局中引用颜色

T.V*_*ert 1 layout android

这是我的styles.xml 文件

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">#6d3655</item>
    <item name="colorPrimaryDark">#442142</item>
    <item name="colorAccent">#de8573</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
Run Code Online (Sandbox Code Playgroud)

我想从我的布局访问 colorPrimary。例如,我尝试将颜色应用于某些 TextField。我将“textColor”属性设置为“@style/AppTheme.colorPrimary”,但它不起作用。为什么?

Mar*_*ski 5

您不必将颜色另存为colors.xml. 您可以直接从主题中引用颜色(或任何其他值),如下所示:

android:textColor="?attr/colorPrimary"
Run Code Online (Sandbox Code Playgroud)

?attr/ 是访问主题属性值的语法。