Android自定义View

本文详细介绍了自定义Android View的三步流程:定义属性、获取属性及重写绘制方法。通过实例展示了如何创建一个带有标题的自定义View,并介绍了如何在布局文件中使用自定义属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义View一般有三个步骤:
1、自定义View的属性
2、构造方法中获取自定义属性
3、重写onDraw()方法

1、自定义View的属性,首先在res/values/ 下建立一个attrs.xml , 在里面定义属性和声明整个样式
例如定义一个标题文字的属性包含:文字内容、文字颜色、文字大小

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="titleText" format="string" />
    <attr name="titleTextColor" format="color" />
    <attr name="titleTextSize" format="dimension" />
    <declare-styleable name="Title">
        <attr name="titleText" />
        <attr name="titleTextColor" />
        <attr name="titleTextSize" />
    </declare-styleable>
</resources>

然后可以在布局中使用自定义的View,
首先要引用命名空间xmlns:app=”http://schemas.android.com/apk/res-auto”
也可以使用xmlns:myWidget=”http://schemas.android.com/apk/res/com.freemud.widget.WidgetTitle”
但是如果你的Moudle是作为一个lib使用时,引用第二种方法会提示找不到自定义属性

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.freemud.widget.WidgetTitle
        android:id="@+id/tb_main"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:padding="3dp"
        app:titleText="标题"  
        app:titleTextColor="#ffffff"  
        app:titleTextSize="24sp" >
    </com.freemud.widget.WidgetTitle>
</RelativeLayout>

2、在View构造方法中获取自定义样式属性

    private Rect mBound;  
    private Paint mPaint;
    /** 
     * 标题内容
     */  
    private String mTitleText;  
    /** 
     * 标题颜色 
     */  
    private int mTitleTextColor;  
    /** 
     * 标题字体大小 
     */  
    private int mTitleTextSize;  

    public WidgetTitle(Context context)  
    {  
        this(context, null);  
    }  

    public WidgetTitle(Context context, AttributeSet attrs)  
    {  
        this(context, attrs, 0);  
    }  

    /** 
     * 获得自定义的样式属性
     * @param context 
     * @param attrs 
     * @param defStyle 
     */  
    public WidgetTitle(Context context, AttributeSet attrs, int defStyle)  
    {  
        super(context, attrs, defStyle);  
        // 获取自定义样式属性 
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.WidgetTitle, defStyle, 0);  
        int n = a.getIndexCount();  
        for (int i = 0; i < n; i++)  
        {  
            int attr = a.getIndex(i);  
            switch (attr)  
            {  
            case R.styleable.WidgetTitle_titleText:  
                mTitleText = a.getString(attr);  
                break;  
            case R.styleable.WidgetTitle_titleTextColor:
                mTitleTextColor = a.getColor(attr, Color.WHITE);  
                break;  
            case R.styleable.WidgetTitle_titleTextSize:  
                mTitleTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(  
                        TypedValue.COMPLEX_UNIT_SP, 24, getResources().getDisplayMetrics()));  
                break;  
            }  
        }  
        a.recycle();  
        mPaint = new Paint();  
        mPaint.setTextSize(mTitleTextSize);  
        mBound = new Rect();  
        mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound); 
    }  

3、重写onDraw()方法

@Override  
    protected void onDraw(Canvas canvas)  
    {  
        mPaint.setColor(Color.YELLOW);  
        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);  
        mPaint.setColor(mTitleTextColor);  
        canvas.drawText(mTitleText, getWidth() / 2 - mBound.width() / 2, getHeight() / 2 + mBound.height() / 2, mPaint);  
    }  

下一章会尝试做一个包含左右按钮的TopTitle,并说一下忘记的onMesure()方法

新手自学android step-by-step,转载请注明出处,谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值