Muh*_*mar 5 java android android-drawable android-styles
我的主题定义为
<style name="AppThemeRed" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimaryRed</item>
<item name="colorPrimaryDark">@color/colorPrimaryDarkRed</item>
<item name="colorAccent">@color/colorAccentRed</item>
</style>
Run Code Online (Sandbox Code Playgroud)
在我的 XML 布局中,我正在做
<android.support.design.widget.AppBarLayout
android:background="?attr/colorPrimary"
android:id="@+id/topBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
Run Code Online (Sandbox Code Playgroud)
每当我更改任何主题时,colorPrimary 都会发生变化
但是,如果我在 drawable 中添加了相同的东西,例如
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="?attr/colorPrimary" />
<corners android:radius="5dp"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="?attr/colorPrimary" />
<corners android:radius="5dp"/>
</shape>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)
它因无法膨胀视图而崩溃,该视图的背景设置为 @drawabe/xxxx
如何在我的 XML drawable 中定义主题颜色属性
只需更换...
android:color="?attr/colorPrimary"
Run Code Online (Sandbox Code Playgroud)
到...
android:color="@color/colorPrimaryRed"
Run Code Online (Sandbox Code Playgroud)
更新
无法在 API 21 以下的 xml 可绘制对象中引用属性。为了制作您的主题,您需要:
每个主题创建一个 xml 可绘制对象。使用 @color 标签或 #RGB 格式直接将所需的颜色包含到您的可绘制对象中。
在 attrs.xml 中为您的可绘制对象创建一个属性。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Attributes must be lowercase as we want to use them for drawables -->
<attr name="my_drawable" format="reference" />
</resources>
Run Code Online (Sandbox Code Playgroud)
将您的可绘制对象添加到您的 theme.xml 中。
<style name="MyTheme" parent="@android:style/Theme.NoTitleBar">
<item name="my_drawable">@drawable/my_drawable</item>
</style>
Run Code Online (Sandbox Code Playgroud)
使用您的属性在布局中引用您的可绘制对象。
<TextView android:background="?my_drawable" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2633 次 |
| 最近记录: |