main.xml <?xml version="1.0" encoding="utf-8"?> <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" xmlns:apps="http://schemas.android.com/apk/res-auto" //studioxmlns:apps="http://schemas.android.com/apk/包名" //eclipseandroid:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.a05view.MainActivity"> <com.example.a05view.MyView android:layout_width="match_parent" android:layout_height="match_parent" apps:my_age="100" apps:my_name="sam" apps:my_bg="@drawable/shopping" /></RelativeLayout>
//attra的属性集合<?xml version="1.0" encoding="utf-8"?> <resources> <!--定义名字为MyView的属性集合--> <declare-styleable name="MyView"> <!--定义名字为my_age,类型为integer的属性--> <attr name="my_age" format="integer"/> <attr name="my_name" format="string"/> <attr name="my_bg" format="reference|color"/> </declare-styleable> </resources>package com.example.a05view; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.View; /** * Created by Administrator on 2016/12/22. */ public class MyView extends View { private String my_name; private int my_age; private Bitmap my_bg; public MyView(Context context, AttributeSet attrs) { super(context, attrs); //获取属性的三种方式 //1.用命名空间获取 String age= attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","my_age"); String name= attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","my_name"); String bg= attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","my_bg"); // Log.d("MyView", "==="+age+"=="+name+"=="+bg); //2.遍历属性集合 for (int i =0;i<attrs.getAttributeCount();i++){ // Log.d("MyView", "==="+attrs.getAttributeName(i)+"=="+attrs.getAttributeValue(i)); } //3.使用系统工具,获取属性(记住第三种就行) TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.MyView); for (int i =0;i<typedArray.getIndexCount();i++){ int index=typedArray.getIndex(i); switch (index){ case R.styleable.MyView_my_age: my_age=typedArray.getInt(index,0); break; case R.styleable.MyView_my_name: my_name=typedArray.getString(index); break; case R.styleable.MyView_my_bg: Drawable bit= typedArray.getDrawable(index); BitmapDrawable bit2= (BitmapDrawable) bit; my_bg=bit2.getBitmap(); break; } }typedArray.recycle();//回收
} @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint paint=new Paint(); paint.setAntiAlias(true); canvas.drawText("name=="+my_name+"==age=="+my_age,50,50,paint); canvas.drawBitmap(my_bg,50,60,paint); }}
Android 自定义属性 view (继承View)
最新推荐文章于 2021-05-27 06:36:00 发布