WEB开发网
开发学院手机开发Android 开发 Android 自定义View 阅读

Android 自定义View

 2010-09-27 02:04:00 来源:WEB开发网   
核心提示:float paddingTop = params.getDimension(R.styleable.MyView_textPaddingTop, 21);setPaddings(paddingLeft, paddingTop);}@Overrideprotected void onDraw(Canvas canvas

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 2 

Tags:Android 定义 View

编辑录入:coldstar [复制链接] [打 印]
赞助商链接