Android 主题中的StyledAttributes未正确加载
我在一个名为字体的文本视图中添加了一个自定义属性。它会自动加载自定义字体 xml示例:Android 主题中的StyledAttributes未正确加载,android,android-styles,android-custom-attributes,Android,Android Styles,Android Custom Attributes,我在一个名为字体的文本视图中添加了一个自定义属性。它会自动加载自定义字体 xml示例: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.egeniq.widget.TextView
android:layout_width="match_parent"
android:layout_height="44dp"
app:font="MyriadPro"
android:text="@string/login_welcome" />
</RelativeLayout>
R.styleable.TextView声明如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="font" format="string" />
<!-- TextView -->
<declare-styleable name="TextView">
<attr name="font" />
</declare-styleable>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.BicycleBuddy" parent="android:Theme.Light.NoTitleBar">
<item name="android:textViewStyle">@style/Widget.TextView</item>
</style>
<!-- Override defaults -->
<style name="Widget.TextView" parent="@android:style/Widget.TextView">
<item name="font">MyriadPro</item>
</style>
</resources>
该主题包含在清单中:
<application
android:name=".Application"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.BicycleBuddy" >
</application>
没有错误。加载自定义小部件并点击断点。不过就像上面说的。如果这样使用,“customFont”属性在读取后为空
堆栈的屏幕截图:第一个(customFont不为null)是XML本身包含app:font=”“
属性的地方。第二,它不包括此属性,但应该从样式中加载此属性
应用程序:font=“””>
应用程序:font=“””>
视图元素不同的原因是因为它们只是两个不同的文本视图。拥有完全相同的财产。只有一个包含app:font=”“
,另一个不包含。断点按我预期的顺序一个接一个地命中
正如你所看到的,实际上一切都是平等的。甚至styledAttrs后面的ID也是相同的。但是,如果它没有在XML中显式声明font属性,则不会加载它
我希望有人能直接看到我失去了什么联系。但我确实意识到这是一个相当模糊的问题
请注意,com.egeniq.*是附加到我的项目的自定义库。styles.xml在“主项目”中声明,而attrs.xml来自库项目您解决过这个问题吗?不幸的是,没有。由于时间限制,我最终没有像我喜欢的那样使用样式,只是在布局xml中声明了属性。
<application
android:name=".Application"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.BicycleBuddy" >
</application>