官方文档

/Myselfcomponent/res/values/attrs.xml

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyView"
        >
        <attr name="textColor" format="color"/>
        <attr name="textSize" format="dimension" />
        <attr name="text" format="string"></attr>
    </declare-styleable>
</resources>

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<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"
    android: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.myselfcomponent.MainActivity" xmlns:my="http://schemas.android.com/apk/res/com.example.myselfcomponent">
 
    <com.example.myselfcomponent.MyView 
        android:id="@+id/MyView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        my:textColor="#00ffff"
        my:textSize="20sp"
        my:text="自定义组件"
         
        />
 
</RelativeLayout>
<!-- my:textColor="#00ffff"
    的前缀跟命名空间有关系
    (tools:context="com.example.myselfcomponent.MainActivity" xmlns:my="http://schemas.android.com/apk/res/com.example.myselfcomponent">),
    可以自定义
 -->

MyView

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.example.myselfcomponent;
 
import android.R.integer;
import android.R.style;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
/**
 * 自定义组件
 * */
public class MyView extends View{
    //画笔
    Paint paint;
    String text;
 
    public MyView(Context context) {
        super(context);
        paint=new Paint();
        paint.setColor(Color.BLACK);
        paint.setTextSize(28);
         
        // TODO Auto-generated constructor stub
    }
    public MyView(Context context,AttributeSet attrs) {
        // TODO Auto-generated constructor stub
        super(context, attrs);
        paint=new Paint();
        TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.MyView);
        int color=typedArray.getColor(R.styleable.MyView_textColor, 0xFFFFFF);
        float size=typedArray.getDimension(R.styleable.MyView_textSize, 20);
        text=typedArray.getString(R.styleable.MyView_text);
        paint.setColor(color);
        paint.setTextSize(size);
        //关闭资源
        typedArray.recycle();
    }
    /**
     * 初始化组件时被触发,进行组件的渲染
     * Canvas   画布
     * */
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        //设置画笔风格为实心
        paint.setStyle(Style.FILL);
        //绘制正方形
        canvas.drawRect(new Rect(10,10,90,90), paint);
        paint.setColor(Color.BLUE);
        canvas.drawText(text, 10110, paint);
    }
 
}

MainActivity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.example.myselfcomponent;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
 
public class MainActivity extends Activity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

android自定义组件


 本文转自 matengbing 51CTO博客,原文链接:http://blog.51cto.com/matengbing/1883078


相关文章: