码迷,mamicode.com
首页 > 移动开发 > 详细

Android自定义 view属性

时间:2016-04-12 18:56:01      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

第一种 

/MainActivity/res/values/attrs.xml

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyCircle">
<attr name="radius" format="integer"/>
<attr name="mycolor" format="reference|color"/>
</declare-styleable>
</resources>

   

/MainActivity/src/com/ch/myviews/MyCircle.java

   

package com.ch.myviews;

import com.ch.myviewattr.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class MyCircle extends View {
private Paint paint;
private int radius;

public MyCircle(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
//获得自定义的属性,即获得自定义的半径
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyCircle);
radius = ta.getInt(R.styleable.MyCircle_radius, 0);


}
//绘制的方法
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
paint = new Paint();
paint.setColor(Color.RED);

// canvas.drawColor(Color.BLACK);
canvas.drawCircle(getMeasuredWidth()/2, getMeasuredHeight()/2, radius, paint);
super.onDraw(canvas);
}

}

 

/MainActivity/res/layout/activity_main.xml

   

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:myattr="http://schemas.android.com/apk/res/com.ch.myviewattr"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<com.ch.myviews.MyCircle
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
myattr:radius="200"
/>

</RelativeLayout>

 

第二种

/MainActivity/res/values/attrs.xml

   
 

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyCircle">
<attr name="radius" format="integer"/>
<attr name="mycolor" format="reference|color"/>
</declare-styleable>
</resources>

/MainActivity/src/com/ch/myviews/MyCircle.java

   
 

package com.ch.myviews;

import com.ch.myviewattr.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class MyCircle extends View {
private Paint paint;
private int radius;
private final static String NAMESPACE = "aaa1407a";
public MyCircle(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
//获得自定义的属性,即获得自定义的半径

radius = attrs.getAttributeIntValue(NAMESPACE, "radius", 0);
}
//绘制的方法
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
paint = new Paint();
paint.setColor(Color.RED);

// canvas.drawColor(Color.BLACK);
canvas.drawCircle(getMeasuredWidth()/2, getMeasuredHeight()/2, radius, paint);
super.onDraw(canvas);
}

}

/MainActivity/res/layout/activity_main.xml

   

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:myattr="aaa1407a"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<com.ch.myviews.MyCircle
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
myattr:radius="200"
/>

</RelativeLayout>

 

Android自定义 view属性

标签:

原文地址:http://www.cnblogs.com/nanze/p/5383520.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!