Android: 如何在另一个布局的 XML 中使用样式中的颜色
Android: how to use color from style in XML of another layout
我在XML得到了这个款式:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base"/>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">@color/teal_500</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">@color/teal_700</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">@color/light_blue_500</item>
<!-- needed to suppress the old ActionBar when using the new Toolbar -->
<item name="android:windowActionBar">false</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight & colorSwitchThumbNormal. -->
</style>
</resources>
问题:
在 XML 的 Fragment
中,我正在使用 TextView
,我想将 "colorPrimary"
从主题。但是怎么办?
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_input"
android:textColor="@android:color/black" <!-- use theme color here-->
android:fontFamily="sans-serif-bold"
android:text="some text"
android:textSize="24dp"/>
你可以这样使用
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_input"
android:textColor="?attr/colorPrimary"
android:fontFamily="sans-serif-bold"
android:text="some text"
android:textSize="24dp"/>
我在XML得到了这个款式:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base"/>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">@color/teal_500</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">@color/teal_700</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">@color/light_blue_500</item>
<!-- needed to suppress the old ActionBar when using the new Toolbar -->
<item name="android:windowActionBar">false</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight & colorSwitchThumbNormal. -->
</style>
</resources>
问题:
在 XML 的 Fragment
中,我正在使用 TextView
,我想将 "colorPrimary"
从主题。但是怎么办?
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_input"
android:textColor="@android:color/black" <!-- use theme color here-->
android:fontFamily="sans-serif-bold"
android:text="some text"
android:textSize="24dp"/>
你可以这样使用
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_input"
android:textColor="?attr/colorPrimary"
android:fontFamily="sans-serif-bold"
android:text="some text"
android:textSize="24dp"/>