【发布时间】:2019-04-11 15:23:43
【问题描述】:
我在自定义视图 defStyleAttr 上苦苦挣扎。 (简短说明我使用 Preference 作为示例,因为它与 Google 使用它的方式相同)
因此,对于 Android 提供的几乎每个 View 或 Preference,您都会有一个类似 this 的构造函数:
public SeekBarPreference(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.seekBarPreferenceStyle);
}
这将默认样式属性定义为R.attr.seekBarPreferenceStyle。
如果您现在查看definition,您会发现:
<attr name="seekBarPreferenceStyle" format="reference" />
到现在一切都清楚了。但是这个属性以某种方式链接到theme:
<resources>
<style name="PreferenceThemeOverlay">
<!-- ... -->
<item name="seekBarPreferenceStyle">@style/Preference.SeekBarPreference.Material</item>
<!-- ... -->
</style>
<!-- ... -->
</resources>
然后最终将 style 与所需的布局资源 id 链接起来,该资源 id 将被移交给要膨胀的超类:
<style name="Preference.SeekBarPreference.Material">
<item name="android:layout">@layout/preference_widget_seekbar_material</item>
<!-- ... -->
</style>
很遗憾,我无法找到有关主题 PreferenceThemeOverlay 如何与属性 seekBarPreferenceStyle 相关联的提示。
那么这两者是如何联系起来的呢?
【问题讨论】:
标签: android xml android-custom-view declare-styleable