Android:在styles.xml中创建自己的属性名称
我需要一些帮助,我已经浏览了一段时间,但是我找到的主题都不能解决我的问题。希望你能帮助我 我们开始:我有一个自定义视图,我们称之为CustomView。 它还有一些自定义属性,在attrs.xml文件中定义,如下所示:Android:在styles.xml中创建自己的属性名称,android,attributes,styling,Android,Attributes,Styling,我需要一些帮助,我已经浏览了一段时间,但是我找到的主题都不能解决我的问题。希望你能帮助我 我们开始:我有一个自定义视图,我们称之为CustomView。 它还有一些自定义属性,在attrs.xml文件中定义,如下所示: <declare-styleable name="CustomView"> <attr name="customBackgroundColor" format="color"/> <attr name="customTextColor
<declare-styleable name="CustomView">
<attr name="customBackgroundColor" format="color"/>
<attr name="customTextColor" format="color"/>
<attr name="customWhatever" format="dimension"/>
</declare-styleable>
此视图是我要创建的库的一部分,因此我可以在多个项目中使用它
有趣的是:实际上,我经常使用这个视图,所以我想在styles.xml中定义一个样式来定义它的属性,这样我就不必在使用这个视图编辑属性的每个xml布局文件中都使用它。大概是这样的:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="customViewStyle">@style/myCustomViewStyle</item>
</style>
<style name="myCustomViewStyle" parent="CustomViewStyle">
<item name="customBackgroundColor">@color/red</item>
<item name="customTextColor">@color/blue</item>
<item name="customWhatever">@dimen/someHeight</item>
</style>
@颜色/原色
@颜色/原色暗
@颜色/颜色重音
@样式/myCustomViewStyle
@颜色/红色
@颜色/蓝色
@尺寸/高度
所以我的问题是:如何定义这个“customViewStyle”键,如果可能的话,如何从中获取信息
奖励:我可以通过以下操作创建此“customViewStyle”键:
<attr name="customViewStyle" format="reference"/>
但我还没有发现如何利用它
如何定义此“customViewStyle”键
,任何
标记的外部
但我还没有发现如何利用它
创建具有所需特性的样式。您已经在帖子中使用了
MyCustomViewStyle
条目,但是您给了它一个不存在的父样式(据我所知)。我可能会使用`parent=“android:Widget”,除非其他更合适的方法
我假定您的自定义视图类已经使用TypedArray
从XML中读取属性:
TypedArray a = context.obstainStyleAttributes(attrs, R.styleable.CustomView,
R.attr.customViewStyle, 0);
将最后一个参数替换为刚才定义的默认样式:
TypedArray a = context.obstainStyleAttributes(attrs, R.styleable.CustomView,
R.attr.customViewStyle, R.style.MyCustomViewStyle);
“你给了它一种根本不存在的父样式”是的,我知道,我只是想展示我希望我的代码是什么样的,但你接下来说的只是我想要的。先生,你应该得到一块饼干!谢谢兄弟,现在我可以回去工作了!