public class MyView extends View
{
public MyView(Context context, AttributeSet attrs)
{
super(context, attrs);
//获取到自定义的属性
TypedArray ta=context.obtainStyledAttributes(attrs, R.styleable.MyView);
int color=ta.getColor(R.styleable.MyView_rect_color, 0xffff0000);
setBackgroundColor(color);
//必须手动回收ta
ta.recycle();
}
public MyView(Context context)
{
super(context);
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
//定义一个declare-styleable标签,在里面设置attr属性
<declare-styleable name="MyView">
<attr name="rect_color" format="color"/>
</declare-styleable>
</resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
//自定义一个MyView的命名空间
xmlns:gu="http://schemas.android.com/apk/res/com.gu.myrect"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.gu.myrect.MyView
android:layout_width="100dp"
android:layout_height="100dp"
//根据自定义的命名空间和我们在attrs中设置的属性,自定义属性值
gu:rect_color="#cc99cc" />
</LinearLayout>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有