Android视图自定义属性

来源:互联网 发布:功夫后一计划软件 编辑:程序博客网 时间:2024/05/23 15:44

1、在values文件夹下生成attr文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyView1">
        <attr name="mycolor" format="color"></attr>
    </declare-styleable>
</resources>/


2、在定义的view中,可将布局文件中的定义view的属性解析

public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.MyView1);

int c=array.getColor(R.styleable.MyView1_mycolor, 0xffff0000);
setBackgroundColor(c);

array.recycle();
}


3、可在xml的自定义view设置自定义的属性:

命名空间: xmlns:zww="http://schemas.android.com/apk/res/com.example.animation" //应用包名

  <paint.MyView
        android:id="@+id/myView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
       zww:mycolor="#66ff00ff"
         />

0 0
原创粉丝点击