现在的位置: 首页 > 综合 > 正文

android自定义View(转)

2013年02月01日 ⁄ 综合 ⁄ 共 3101字 ⁄ 字号 评论关闭

android自定义View(转)

andoird 2010-04-07 10:54:20 阅读339 评论0字号:

统一的用户界面是可以使得应用程序更友好。要做到用户界面的统一,我们就必须用到风格(style)和主题(theme),以下非原创,仅供个人学习参考。

在values/attrs.xml中:
<?xml version="1.0" encoding="utf-8"?>
<resources>

<declare-styleable name="MyView">
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
<attr name="imgBackground" format="integer" />
<attr name="textPaddingLeft" format="dimension"/>
<attr name="textPaddingTop" format="dimension"/>
</declare-styleable>

</resources>

package com.test.TestView;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {

 

   private Paint mPaint;

    private Context mContext;

    private String mStr;

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initMyView();
        TypedArray params = context.obtainStyledAttributes(attrs,
                R.styleable.MyView);
        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, 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);
    }

    void setText(String text) {
        mStr = text;
    }

    private void setPaddings(float paddingLeft, float paddingTop) {

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

在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/com.test.TestView"   //此处注意,是指apk的package,而不是控件所有的package.
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<com.test.TestView.MyView
    android:id="@+id/myview"
    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"
/>
</LinearLayout>

在自己的Activity 中
public class TestView extends Activity {
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        MyView mTextView=(MyView)findViewById(R.id.myview);
        mTextView.setText("自定义的View");
    }
}

抱歉!评论已关闭.