Android 中自定义View的应用
2011-09-07 10:17
381 查看
首先新建一个Android 工程 命名为ViewDemo .
然后自定义一个View 类,命名为MyView(extends View) .代码如下:
文章转自:/article/4120372.html
然后自定义一个View 类,命名为MyView(extends View) .代码如下:
package com.android.tutor; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.Paint.Style; import android.util.AttributeSet; import android.view.View; public class MyView extends View { private Paint mPaint; private Context mContext; private static final String mString = "Welcome to Mr Wei's blog"; public MyView(Context context) { super(context); } public MyView(Context context,AttributeSet attr) { super(context,attr); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); mPaint = new Paint(); //设置画笔颜色 mPaint.setColor(Color.RED); //设置填充 mPaint.setStyle(Style.FILL); //画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标 canvas.drawRect(new Rect(10, 10, 100, 100), mPaint); mPaint.setColor(Color.BLUE); //绘制文字 canvas.drawText(mString, 10, 110, mPaint); } }
然后将我们自定义的View 加入到main.xml 布局文件中,代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <com.android.tutor.MyView android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <com.android.tutor.MyView android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
文章转自:/article/4120372.html
相关文章推荐
- Android高手进阶教程(三)之----Android 中自定义View的应用
- Android自定义view组件应用
- android之视频播放系统VideoView和自定义VideoView控件的应用
- 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)
- Android高手进阶教程(三)之----Android 中自定义View的应用.
- android开发:Android 中自定义View的应用
- Android高手进阶教程(三)之----Android 中自定义View的应用.
- android自定义ViewPager之——3D效果应用
- Android应用开发中自定义ViewGroup的究极攻略
- 【Android】自定义View、画家(画布)Canvas与画笔Paint的应用——画图、涂鸦板app的实现
- Android高手进阶教程(三)之----Android 中自定义View的应用
- Android高手进阶教程(三)之----Android 中自定义View的应用.
- Android应用自定义View绘制方法手册
- Android 中自定义View的应用.
- Android高手进阶教程(三)之----Android 中自定义View的应用.
- Android高手进阶教程(三)之----Android 中自定义View的应用.
- Android应用开发之自定义View触摸相关工具类全解
- Android高手进阶教程(三)之----Android 中自定义View的应用.
- Android基础教程(九)之自定义下拉菜单模式----Spinner与setDropDownViewResource的应用
- Android自定义ViewGroup中LayoutParam的应用