自定义view和自定义属性(献给和我一样的小白)

一、自定义View
     体现在布局文件中如下:
   
  <?xml version="1.0" encoding="utf-8"?>
	<A>
	   <B></B>
	</A>


     其中 A extends LinerLayout
          B extends View
(忽略布局)


做法:


1、创建一个类MyView 集成 View类。
public class MyBrick extends View

2、重载父类中的三个构造方法和onDraw方法。
public MyBrick(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}


	public MyBrick(Context context, AttributeSet attrs) {
		super(context, attrs);	
	}


	public MyBrick(Context context) {
		super(context);
	}


3、使用Paint设置view中的东西(例如我们的view 需要显示一个红色的圆角矩形)
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Paint paint = new Paint();
		paint.setColor(Color.RED);      //设置颜色
		paint.setStyle(Style.FILL);     //布局填充
		canvas.drawRoundRect(new RectF(50, 0, 100, 100), 15f, 20f, paint);   //画出圆角矩形
	}


4、在布局文件里面使用如下:
	<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	    xmlns:tools="http://schemas.android.com/tools"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent" >


		    <com.example.brickbreaker.childview.MyBrick
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" >
		    </com.example.brickbreaker.childview.MyBrick>


	</RelativeLayout>



二、自定义属性


1、在res/values下创建attrs.xml
内容如下:
<?xml version="1.0" encoding="utf-8"?>
	<resources>


	    <declare-styleable name="MyBrick">
		<attr name="joeColor" format="color"></attr>
		<attr name="joeTextSize" format="dimension"></attr>
	    </declare-styleable>


	</resources>



   其中,declare-styleable 中的name 属性值是我们自定义的view的名称。
         attr标签中的name是我们自定义属性的名字,format表示自定义属性的类型或者格式。
 例如:joeColor是color属性


   format的一些其他常见值如下:
   
reference   表示引用 ,例如R.id.xx。
string   表示字符串
color   表示颜色值
dimension   表示尺寸值
boolean   表示布尔值
integer   表示整型值
float   表示浮点值
fraction   表示百分数
enum   表示枚举值
flag   表示位运算


        2、在自定义view类中使用自定义属性。
在自定义view的构造方法  类名(Context context ,AttributeSet attrs)中设置属性的
路径和默认值

	public MyBrick(Context context, AttributeSet attrs) {
			super(context, attrs);
			Paint paint = new Paint();
			TypedArray a = context.obtainStyledAttributes(attrs,
					R.styleable.MyBrick);
			int color = a.getColor(R.styleable.MyBrick_joeColor, Color.GREEN);
			float dimension = a.getDimension(R.styleable.MyBrick_joeTextSize, 22);
			paint.setColor(color);
			paint.setTextSize(dimension);
			a.recycle();
		}


R.styleable.MyBrick_joeColor 由三部分组成:


R.styleable.MyBrick是attrs.xml中的declare-styleable中的MyBrick


joeColor是自定义的属性,  “_”下划线负责将他们连接到一起。这样,我们在布局文件使用到自定义舒心joeColor时,
虚拟机就可以知道我们是要为自定义VIew添加属性了。

int color = a.getColor(R.styleable.MyBrick_joeColor, Color.GREEN);
		float dimension = a.getDimension(R.styleable.MyBrick_joeTextSize, 22);
		paint.setColor(color);
		paint.setTextSize(dimension);


上面这四句话是设置默认属性。
3、布局文件中使用自定义属性


必须在最外层布局添加
xmlns:joe="http://schemas.android.com/apk/res/com.example.brickbreaker"
joe是自定义的名称,称作“命名空间”,以上面的一行设置代码为例,在布局文件中,如果要给某个自定义view使用自定义属性,要把命名空间
由原来的android换成joe,如下

    <com.example.brickbreaker.childview.MyBrick
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"


				joe:joeColor="#A26FA7" >


			    </com.example.brickbreaker.childview.MyBrick>


com.example.brickbreaker是自定义view所在应用的包名,这个必须的,不能随便写。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值