首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

View种和自定义控件与实例

2013-08-25 
View类和自定义控件与实例?XML文件:RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/

View类和自定义控件与实例

?

XML文件:
<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.itab.tab      android:layout_width="fill_parent"      android:layout_height="49dp"      android:layout_alignParentBottom="true"      /></RelativeLayout>

?

Java文件:
package com.example.itab;public class tab extends View {    private Paint mp;            //声明一个画笔控件    public tab(Context context, AttributeSet attrs) {        super(context, attrs);    }    @Override    protected void onDraw(Canvas canvas) {        mp = new Paint();               // 新建画笔        mp.setStyle(Paint.Style.FILL);       // 设置画笔为实心        Rect r = new Rect();             // 新建一个矩形框        this.getDrawingRect(r);           // 填充矩形的各个位置属性        canvas.drawColor(0XFF000000);       // 设置画布颜色        mp.setColor(0X00FF00);           // 设置画笔颜色        /* 绘制一条直线,参数依次为:开始的横坐标,开始的纵坐标,结束的横坐标,结束的纵坐标,坐标由矩形来决定,用mp画笔来绘制 */        canvas.drawLine(r.left, r.centerY(), r.right, r.centerY(), mp);        super.onDraw(canvas);    }}

热点排行