• Android自定义控件的实现步骤(二)


    前天已经写了一个关于自定义控件的实现步骤的博客,这个是附上代码的详细版本

    首先,我们得新建一个attrs.xml的资源文件,在上面添加我们将要自定义控件的额外属性,就是自定义控件的自定义属性,具体代码如下:

    <resources>
    
        <declare-styleable name="TestView">
            <attr name="textColor" format="color"></attr>
            <attr name="textSize" format="dimension"></attr>
            <attr name="imgBackground" format="integer"></attr>
            <attr name="topBorder" format="boolean"></attr>
            <attr name="bottomBorder" format="boolean"></attr>
            <attr name="leftBorder" format="boolean"></attr>
            <attr name="rightBorder" format="boolean"></attr>
        </declare-styleable>
    
    </resources>

    然后我们在定义一个自己控件的类,要继承相应的控件比如说你想自定义一个TextView控件,你就得继承TextView这个类,然后在类中定义与你上面属性相对应的变量,然后通过TypedArray类获得相应的属性的值,然后也就是最主要的一点,我们得在OnDraw函数中使用,具体代码如下:

    public class MyView extends TextView {
    
        private Paint mTextPaint;
        private Paint mBorderPaint;
        private Context mContext;
        private boolean mTopBorder;
        private boolean mBottomBorder;
        private boolean mLeftBorder;
        private boolean mRightBorder;
    
        public MyView(Context context, AttributeSet attrs) {
            super(context, attrs);
            // TODO Auto-generated constructor stub
            mContext = context;
            initMyView();
            // 对于我们自定义的类中,我们需要使用一个名为obtainStyledAttributes的方法来获取我们的定义。
            TypedArray params = context.obtainStyledAttributes(attrs,
                    R.styleable.TestView);
            // 得到自定义控件的属性值。
            int backgroudId = params.getResourceId(
                    R.styleable.TestView_imgBackground, 0);
            if (backgroudId != 0)
                setBackgroundResource(backgroudId);
            int textColor = params.getColor(R.styleable.TestView_textColor,
                    0XFFFFFFFF);
            setTextColor(textColor);
            float textSize = params.getDimension(R.styleable.TestView_textSize, 40);
            setTextSize(textSize);
            
            mTopBorder = params.getBoolean(R.styleable.TestView_topBorder, true);
            mLeftBorder = params.getBoolean(R.styleable.TestView_leftBorder, true);
            mBottomBorder = params.getBoolean(R.styleable.TestView_bottomBorder, true);
            mRightBorder = params.getBoolean(R.styleable.TestView_rightBorder, true);
            
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            if (mTopBorder == true)
                canvas.drawLine(0, 0, this.getWidth() - 1, 0, mBorderPaint);
            if (mRightBorder == true)
                canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1,
                        this.getHeight() - 1, mBorderPaint);
            if (mBottomBorder == true)
                canvas.drawLine(this.getWidth() - 1, this.getHeight() - 1, 0,
                        this.getHeight() - 1, mBorderPaint);
            if (mLeftBorder == true)
                canvas.drawLine(0, this.getHeight() - 1, 0, 0, mBorderPaint);
        }
    
        public void initMyView() {
            mTextPaint = new Paint();
            mTextPaint.setColor(Color.WHITE);
    
            mBorderPaint = new Paint();
            mBorderPaint.setColor(android.graphics.Color.BLACK);
        }
    
        public void setTextColor(int textColor) {
            mTextPaint.setColor(0XFFAABBCC);
        }
    
        public void setTextSize(float textSize) {
            mTextPaint.setTextSize(textSize);
        }
    
        public void setTopBorder(boolean mTopBorder) {
            this.mTopBorder = mTopBorder;
        }
    
        public void setBottomBorder(boolean mBottomBorder) {
            this.mBottomBorder = mBottomBorder;
        }
    
        public void setLeftBorder(boolean mLeftBorder) {
            this.mLeftBorder = mLeftBorder;
        }
    
        public void setRightBorder(boolean mRightBorder) {
            this.mRightBorder = mRightBorder;
        }
    
        public void setPaddings(float paddingLeft, float paddingTop) {
            setPadding((int) paddingLeft, (int) paddingTop, 0, 0);
        }
    }

    最后一步就是在你的Activity的布局中使用你所定义的控件,具体的使用方法就是你自定义控件的类所在的包的包名加上类名(使用的时候,在设置自定义属性的时候是没有提示的,得自己记住自己设定的属性)

     <myControl.MyView
                        android:id="@+id/TextView_Tue"
                        android:layout_width="@dimen/cell_width"
                        android:layout_height="40dp"
                        android:layout_weight="1"
                        android:background="@drawable/solid"
                        android:text="" />

    注:最后的注意的是在使用自定义控件的时候,必须在前面加上自己属性的引用,如下代码(入第二行所示):

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:myView="http://schemas.android.com/apk/res/com.zsxy_schedule"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
  • 相关阅读:
    Auto X2021 K Increasing Sequence
    拉普拉斯平滑处理 Laplace Smoothing
    博学之
    Python-生成音乐-pyshnth
    Python-Kivy ImportError: DLL load failed: 找不到指定的模块
    Python-Word模板填充-docxtpl
    Python-文字转语音-pyttsx3
    Virtual Box中Ubuntu使用"桥接网卡"不能联网而使用"网络地址转换(NAT)"却可以上网
    STM32的HAL库中的DMA_FLAG_TCIF3_7等几个宏定义的含义
    Linux下编写互相通信的驱动模块并将其加入到内核中
  • 原文地址:https://www.cnblogs.com/zds-song/p/3850082.html
Copyright © 2020-2023  润新知