import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
public class MyCustomView extends View {
/**
* 第一个构造函数
*/
public MyCustomView(Context context) {
this(context, null);
}
/**
* 第二个构造函数
*/
public MyCustomView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
/**
* 第三个构造函数
*/
public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO:获取自定义属性
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
}
<resources> <declare-styleable name="MyCustomView"> <attr name="custom_attr1" format="string" /> <attr name="custom_attr2" format="string" /> <attr name="custom_attr3" format="string" /> <attr name="custom_attr4" format="string" /> </declare-styleable> <attr name="custom_attr5" format="string" /> </resources>
public final class R {
public static final class attr {
public static final int custom_attr1=0x7f010038;
public static final int custom_attr2=0x7f010039;
public static final int custom_attr3=0x7f01003a;
public static final int custom_attr4=0x7f01003b;
public static final int custom_attr5=0x7f010000;
}
}
public static final class styleable {
public static final int[] MyCustomView = {
0x7f010038, 0x7f010039, 0x7f01003a, 0x7f01003b
};
public static final int MyCustomView_custom_attr1 = 0;
public static final int MyCustomView_custom_attr2 = 1;
public static final int MyCustomView_custom_attr3 = 2;
public static final int MyCustomView_custom_attr4 = 3;
}
public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView);
String attr1 = ta.getString(R.styleable.MyCustomView_custom_attr1);
String attr2 = ta.getString(R.styleable.MyCustomView_custom_attr2);
String attr3 = ta.getString(R.styleable.MyCustomView_custom_attr3);
String attr4 = ta.getString(R.styleable.MyCustomView_custom_attr4);
Log.e("customview", "attr1=" + attr1);
Log.e("customview", "attr2=" + attr2);
Log.e("customview", "attr3=" + attr3);
Log.e("customview", "attr4=" + attr4);
ta.recycle();
}
public final TypedArray obtainStyledAttributes(AttributeSet set, @StyleableRes int[] attrs) {
return getTheme().obtainStyledAttributes(set, attrs, 0, 0);
}
public TypedArray obtainStyledAttributes(AttributeSet set,
@StyleableRes int[] attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
final int len = attrs.length;
final TypedArray array = TypedArray.obtain(Resources.this, len);
// XXX note that for now we only work with compiled XML files.
// To support generic XML files we will need to manually parse
// out the attributes from the XML file (applying type information
// contained in the resources and such).
final XmlBlock.Parser parser = (XmlBlock.Parser)set;
AssetManager.applyStyle(mTheme, defStyleAttr, defStyleRes,
parser != null ? parser.mParseState : 0, attrs, array.mData, array.mIndices);
array.mTheme = this;
array.mXml = parser;
if (false) {
int[] data = array.mData;
System.out.println("Attributes:");
String s = " Attrs:";
int i;
for (i=0; i<set.getAttributeCount(); i++) {
s = s + " " + set.getAttributeName(i);
int id = set.getAttributeNameResource(i);
if (id != 0) {
s = s + "(0x" + Integer.toHexString(id) + ")";
}
s = s + "=" + set.getAttributeValue(i);
}
System.out.println(s);
s = " Found:";
TypedValue value = new TypedValue();
for (i=0; i<attrs.length; i++) {
int d = i*AssetManager.STYLE_NUM_ENTRIES;
value.type = data[d+AssetManager.STYLE_TYPE];
value.data = data[d+AssetManager.STYLE_DATA];
value.assetCookie = data[d+AssetManager.STYLE_ASSET_COOKIE];
value.resourceId = data[d+AssetManager.STYLE_RESOURCE_ID];
s = s + " 0x" + Integer.toHexString(attrs[i])
+ "=" + value;
}
System.out.println(s);
}
return array;
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent"> <com.kevintan.eventbussample.view.MyCustomView android:id="@+id/id_custom_view" android:layout_width="400dp" android:layout_height="400dp" custom:custom_attr1="attr1_xml" style="@style/TestCustomView"/> </FrameLayout>
<style name="TestCustomView"> <item name="custom_attr1">attr1_style</item> <item name="custom_attr2">attr2_style</item> </style>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent"> <com.kevintan.eventbussample.view.MyCustomView android:id="@+id/id_custom_view" android:layout_width="400dp" android:layout_height="400dp" custom:custom_attr1="attr1_xml" style="@style/TestCustomView"/> </FrameLayout>
<activity> android:name="com.kevintan.eventbussample.MainActivity" android:theme="@style/AppTheme" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="MyCustomViewDefStyleAttr">@style/MyCustomViewDefStyleAttrImpl</item> </style> <style name="MyCustomViewDefStyleAttrImpl"> <item name="custom_attr1">attr1_defStyleAttr</item> <item name="custom_attr2">attr2_defStyleAttr</item> <item name="custom_attr3">attr3_defStyleAttr</item> </style>
public MyCustomView(Context context, AttributeSet attrs) {
// 为defStyleAttr进行赋值
this(context, attrs, R.attr.MyCustomViewDefStyleAttr);
}
public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyCustomView, defStyleAttr, 0);
String attr1 = ta.getString(R.styleable.MyCustomView_custom_attr1);
String attr2 = ta.getString(R.styleable.MyCustomView_custom_attr2);
String attr3 = ta.getString(R.styleable.MyCustomView_custom_attr3);
String attr4 = ta.getString(R.styleable.MyCustomView_custom_attr4);
Log.e("customview", "attr1=" + attr1);
Log.e("customview", "attr2=" + attr2);
Log.e("customview", "attr3=" + attr3);
Log.e("customview", "attr4=" + attr4);
ta.recycle();
}
<style name="MyCustomViewDefStyleRes"> <item name="custom_attr1">attr1_defStyleRes</item> <item name="custom_attr2">attr2_defStyleRes</item> <item name="custom_attr3">attr3_defStyleRes</item> <item name="custom_attr4">attr4_defStyleRes</item> </style>
public MyCustomView(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.MyCustomViewDefStyleAttr);
}
public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// 为defStyleRes进行赋值
TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyCustomView, defStyleAttr, R.style.MyCustomViewDefStyleRes);
String attr1 = ta.getString(R.styleable.MyCustomView_custom_attr1);
String attr2 = ta.getString(R.styleable.MyCustomView_custom_attr2);
String attr3 = ta.getString(R.styleable.MyCustomView_custom_attr3);
String attr4 = ta.getString(R.styleable.MyCustomView_custom_attr4);
Log.e("customview", "attr1=" + attr1);
Log.e("customview", "attr2=" + attr2);
Log.e("customview", "attr3=" + attr3);
Log.e("customview", "attr4=" + attr4);
ta.recycle();
}
public MyCustomView(Context context, AttributeSet attrs) {
// 为了验证defStyleRes的作用,将defStyleAttr设置为0
this(context, attrs, 0);
}
public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyCustomView, defStyleAttr, R.style.MyCustomViewDefStyleRes);
String attr1 = ta.getString(R.styleable.MyCustomView_custom_attr1);
String attr2 = ta.getString(R.styleable.MyCustomView_custom_attr2);
String attr3 = ta.getString(R.styleable.MyCustomView_custom_attr3);
String attr4 = ta.getString(R.styleable.MyCustomView_custom_attr4);
Log.e("customview", "attr1=" + attr1);
Log.e("customview", "attr2=" + attr2);
Log.e("customview", "attr3=" + attr3);
Log.e("customview", "attr4=" + attr4);
ta.recycle();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有