android view构造函数研究
2 public void CustomView(Context context, AttributeSet attrs) {}
3 public void CustomView(Context context, AttributeSet attrs, int defStyle) {}
2 <resources>
3 <style name="customstyle">
4 <item name="android:background">@drawable/bg</item>
5 [... or other style code...]
6 </style>
7 </resources>
2 this(context, attrs, resid);
3 }
2 <resources>
3 <style name="purple">
4 <item name="android:background">#FFFF00FF</item>
5 </style>
6 </resources>
02
03 public class CustomView extends View {
04
05 //C1
06 public CustomView(Context context) {
07 super(context);
08 }
09
10 //C2
11 public CustomView(Context context, AttributeSet attrs) {
12 this(context, attrs, 0);
13 }
14
15 //C3
16 public CustomView(Context context, AttributeSet attrs, int defStyle) {
17 super(context, attrs, defStyle);
18 }
19 }
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:myxmlns="http://schemas.android.com/apk/res/com.your.test"
4 android:orientation="vertical"
5 android:layout_width="fill_parent"
6 android:layout_height="fill_parent" >
7 <com.your.test.CustomView android:layout_width="100px"
8 android:layout_height="100px" />
9 </LinearLayout>
2
3 public class Test extends Activity {
4 @Override
5 public void onCreate(Bundle savedInstanceState) {
6 super.onCreate(savedInstanceState);
7 setContentView(R.layout.main);
8 }
9 }
2 android:layout_height="100px"
3 style="@style/purple"
4 />
2 this(context, attrs, R.style.purple);
3 }
2 <resources>
3 <declare-styleable name="CustomView">
4 <attr name="ourstyle" format="reference" />
5 <attr name="atext" format="string" />
6 </declare-styleable>
7 </resources>
2 android:layout_height="100px"
3 myxmlns:ourstyle="@style/purple"
4 myxmlns:atext="test string"
5 />
2 <item name="ourstyle">@style/purple</item>
3 </style>
2 public CustomView(Context context, AttributeSet attrs) {
3 this(context, attrs, R.attr.ourstyle );
4 }
02 public CustomView(Context context, AttributeSet attrs) {
03 this(context, attrs, attrs.getAttributeNameResource(2));
04 String a1 = ((Integer)R.attr.ourstyle).toString();
05 String a2 = ((Integer)R.styleable.CustomView_ourstyle).toString();
06 String a3 = ((Integer)R.styleable.CustomView[R.styleable.CustomView_ourstyle]).toString();
07 String a4 = ((Integer)R.style.purple).toString();
08 String a5 = ((Integer)attrs.getAttributeNameResource(2)).toString();
09 String a6 = ((Integer)attrs.getAttributeResourceValue(2,0)).toString();
10 String a7 = ((Integer)R.attr.atext).toString();
11 String a8 = ((Integer)R.styleable.CustomView[R.styleable.CustomView_atext]).toString();
12 String a9 = attrs.getAttributeValue(2);
13 String a10 = attrs.getAttributeValue(3);
14 }
02 a2 = 0
03 a3 = 2130771968
04 a4 = 2131034112
05 a5 = 2130771968
06 a6 = 2131034112
07 a7 = 2130771969
08 a8 = 2130771969
09 a9 = @2131034112
10 a10 = test string
2 this(context);
3 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,defStyle, 0);
4 [...other code...]
public TypedArray obtainStyledAttributes (AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)
Return a StyledAttributes holding the attribute values in set that are listed in attrs. In addition, if the given AttributeSet specifies a style class (through the "style" attribute), that style will be applied on top of the base attributes it defines.
Be sure to call StyledAttributes.recycle() when you are done with the array.
When determining the final value of a particular attribute, there are four inputs that come into play:
- Any attribute values in the given AttributeSet.
- The style resource specified in the AttributeSet (named "style").
- The default style specified by defStyleAttr and defStyleRes
- The base values in this theme.
Each of these inputs is considered in-order, with the first listed taking
precedence over the following ones. In other words, if in the AttributeSet you
have supplied <Button textColor="#ff000000">
, then the
button‘s text will always be black, regardless of what is
specified in any of the styles.
Parameters
set | The base set of attribute values. May be null. |
---|---|
attrs | The desired attributes to be retrieved. |
defStyleAttr | An attribute in the current theme that contains a reference to a style resource that supplies defaults values for the StyledAttributes. Can be 0 to not look for defaults. |
defStyleRes | A resource identifier of a style resource that supplies default values for the StyledAttributes, used only if defStyleAttr is 0 or can not be found in the theme. Can be 0 to not look for defaults. |
Returns
- Returns a TypedArray holding an array of the attribute values. Be sure to
call
TypedArray.recycle()
when done with it.
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。