Android提供了强大的可复用组件,但特殊情况需要自己的自定义的View组件,下面自定义一个View。
首先在values/attrs.xml中定义好自定义的View会有哪些属性需要在XML中配置:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView">
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
//在attrs中定义background
<attr name="imgBackground" format="integer" />
<attr name="textPaddingLeft" format="dimension"/>
<attr name="textPaddingTop" format="dimension"/>
</declare-styleable>
</resources>


编写自定义的View-MyView.java,继承View

package test.cuntomizedview;

import java.util.Calendar;

import test.cuntomizedview.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {

private Paint mPaint;

private Context mContext;

private String mStr;
/*如果需要在配置文件中使用该自定义组件,注意一定要重写public **View(Context context, AttributeSet attrs)的构造方法,而不是public **View(Context context)。如果只需要在程序中利用代码使用该组件可以只覆写public **View(Context context)*/
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);

mContext = context;
initMyView();
/*获取在xml中配置的属性值*/
TypedArray params = context.obtainStyledAttributes(attrs,
R.styleable.MyView);
//取得background
int backgroudId = params.getResourceId(
R.styleable.MyView_imgBackground, 0);
if (backgroudId != 0)
setBackgroundResource(backgroudId);
int textColor = params.getColor(R.styleable.MyView_textColor,
0XFFFFFFFF);
setTextColor(textColor);
float textSize = params.getDimension(R.styleable.MyView_textSize, 36);
setTextSize(textSize);
float paddingLeft = params.getDimension(
R.styleable.MyView_textPaddingLeft, 41);
float paddingTop = params.getDimension(
R.styleable.MyView_textPaddingTop, 21);
setPaddings(paddingLeft, paddingTop);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

if(mStr != null) {
canvas.drawText(mStr, 0, 0, mPaint);
}
canvas.drawText("heiheihei", 30, 60, mPaint);
}

private void initMyView() {
mPaint = new Paint();
mPaint.setColor(Color.WHITE);
}

private void setTextColor(int textColor) {
mPaint.setColor(0XFFAABBCC);
}

private void setTextSize(float textSize) {
mPaint.setTextSize(textSize);
}

private void setPaddings(float paddingLeft, float paddingTop) {

setPadding((int)paddingLeft, (int)paddingTop, 0, 0);
}
}

// 注意怎样在attrs中怎样定义background并取得background。

在layout中使用MyView,
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/test.cuntomizedview"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<test.cuntomizedview.MyView android:id="@+id/v"
android:layout_width="fill_parent" android:layout_height="fill_parent"
app:textColor="#FFFFFFFF" app:textSize="40dip"
app:textPaddingLeft="40dip" app:textPaddingTop="40dip"
app:imgBackground="@drawable/bg_time"
/>
</LinearLayout>

更多相关文章

  1. Android之UI- listview特别的属性
  2. Android动画开发——Animation动画效果(网上整理)
  3. android services 使用
  4. Android(安卓)onConfigurationChanged 不执行
  5. Android(安卓)自定义实现日历控件
  6. Android生命周期组件Lifecycle使用详解
  7. Android(安卓)settings.db数据库中添加一条新的默认配置项
  8. android开发积累2-实现自定义android控件
  9. Android(安卓)自定义LayoutManager

随机推荐

  1. Android(安卓)App多个入口的实现方法
  2. Android多分辨率和多屏幕的布局适配详解
  3. 第三部分:Android(安卓)应用程序接口指南-
  4. android makefile(android.mk)分析(序)
  5. Android(安卓)LinearLayout线性布局(上下
  6. Android测试驱动开发实践1
  7. Android(安卓)UI开发第十二篇――动画效
  8. 第2章、搭建Android的开发环境(从零开始学
  9. Android层次架构
  10. mulator: ERROR: no search paths found