<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">1、自定义属性,如果需要在代码中引用,需要使用declare-styleable来定义。在代码中使用obtainStyledAttributes来获取。</span>
详情可参考http://www.cnblogs.com/ufocdy/archive/2011/05/27/2060221.html
2、自定义样式,样式的内容也需要先在属性中定义。有优先级的问题。可参考http://www.cnblogs.com/angeldevil/p/3479431.html#two.one
贴部分上面参考网址的代码做说明:
attr中:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Customize">
<attr name="attr_one" format="string" />
<attr name="attr_two" format="string" />
<attr name="attr_three" format="string" />
<attr name="attr_four" format="string" />
</declare-styleable>
<attr name="CustomizeStyle" format="reference" />
<attr name="CustomizeStyleString" format="string" />
</resources>
style:
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="CustomizeStyle">@style/CustomizeStyleInTheme</item>
</style>
<style name="CustomizeStyleInTheme">
<item name="attr_one">attr one from theme reference</item>
<item name="attr_two">attr two from theme reference</item>
<item name="attr_three">attr three from theme reference</item>
</style>
<style name="DefaultCustomizeStyle">
<item name="attr_one">attr one from defalut style res</item>
<item name="attr_two">attr two from defalut style res</item>
<item name="attr_three">attr three from defalut style res</item>
</style>
<style name="ThroughStyle">
<item name="attr_one">attr one from style</item>
<item name="attr_two">attr two from style</item>
</style>
layout:
<com.example.attrtest.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ad:attr_one="attr one in xml"
ad:CustomizeStyleString="CustomizeStyleString"
style="@style/ThroughStyle"
android:text="@string/hello_world" />
public CustomTextView(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.CustomizeStyle);
Log.i(TAG,"CustomTextView two params");
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Customize, defStyle,R.style.DefaultCustomizeStyle);
String one = a.getString(R.styleable.Customize_attr_one);
String two = a.getString(R.styleable.Customize_attr_two);
String three = a.getString(R.styleable.Customize_attr_three);
String four = a.getString(R.styleable.Customize_attr_four);
Log.i(TAG, "one:" + one);
Log.i(TAG, "two:" + two);
Log.i(TAG, "three:" + three);
Log.i(TAG, "four:" + four);
a.recycle();
}
1、CustomTextView的构造参数有三种形式:一个参数的是在代码中直接new创建时调用,二个参数的是在xml定义组件时会被调用,三个参数的只有被二个参数的调用
2、obtainStyledAttributes的第2个参数是R.styleable.Customize,在attrs.xml中使用declare-styleable定义,表示你想获取的属性集。通过obtainStyledAttributes获取到的属性必然是在attr定义的
3、attr_one,att_two,attr_three,attr_four在layout xml,style中都有定义,优先级:layout xml中直接指定(ad:attr_one) > layout style(style="@style/ThroughStyle" ) > 由defStyleAttr(第三个参数)defStyleRes指定的默认值(第四个参数) > 直接在Theme中指定的值。第三个参数必须要在Theme中指定,且是指向一个Style的引用,如果这个参数传入0表示不向Theme中搜索默认值。第四个参数仅在defStyleAttr为0或defStyleAttr不为0但Theme中没有为defStyleAttr属性赋值时起作用
参考网址给的代码不止为何跑不起来,做了些删改: