Android 自定义控件
来源:互联网 发布:12345数字打字软件 编辑:程序博客网 时间:2024/05/21 18:38
</pre><p></p><p><a target=_blank target="_blank" href="http://blog.csdn.net/lmj623565791/article/details/24252901" style="font-family:Arial; font-size:14px; line-height:26px; white-space:pre">其实是根据鸿洋大神的博客改的</a></p><p></p><h2><span style="font-size:24px">自定义View步骤</span>:</h2><div><span style="white-space:pre"></span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; white-space:pre"></span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">1、自定义View的属性(<strong>res/values/新建XML文件</strong>)如下</span></div><div><span style="font-family:Arial; color:#333333"><span style="font-size:14px; line-height:26px"></span></span><pre name="code" class="html"><?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="CustomTitleView(其实这里的名字并不要求与自定义的View类名一致,但是在用时R.styleable.xxx(这里的名字与xxx一致)_自定义属性名)"> <attr name="titleText" /> <attr name="titleTextColor" /> <attr name="titleTextSize" /> </declare-styleable> </resources>既然自定义了view控件,那么就应该在主布局中使用呀,因此在主布局中引入
注意:主布局中引入时,加上
为了区别于TextView以突显自定义View,我们可以添加一个setOnListener()
xmlns:custom(自己随便起名)="http://schemas.android.com/apk/res/包名(前面是固定的,这里是自定义View项目包名)"当然在引入控件的地方应该引入View的包名和类名全路径
<包名+类名 android:layout_width="200dp" android:layout_height="100dp" custom(这里的custom和前面的xmls:custome定义保持一致):titleText="3712" custom(这里的custom和前面的xmls:custome定义保持一致):titleTextColor="#ff0000" </span> custom(这里的custom和前面的xmls:custome定义保持一致):titleTextSize="40sp" />
2、在自定义View类的构造方法中获得我们自定义的属性(一般实现了三个不同参数个数的构造方法)
在最核心的构造函数中一般会这样
public CustomTitleView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); /** * 获得我们所定义的自定义样式属性 */ TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTitleView, defStyle, 0); int n = a.getIndexCount(); for (int i = 0; i < n; i++) { int attr = a.getIndex(i); switch (attr) { case R.styleable.CustomTitleView_titleText: mTitleText = a.getString(attr); break; case R.styleable.CustomTitleView_titleTextColor: // 默认颜色设置为黑色 mTitleTextColor = a.getColor(attr, Color.BLACK); break; case R.styleable.CustomTitleView_titleTextSize: // 默认设置为16sp,TypeValue也可以把sp转化为px mTitleTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics())); break; } } a.recycle(); /** * 获得绘制文本的宽和高 */ mPaint = new Paint(); mPaint.setTextSize(mTitleTextSize); // mPaint.setColor(mTitleTextColor); mBound = new Rect(); mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound); }
[ 3、在自定义View类中重写onMesure ]可用可不用,根据需要复写该方法(但一般需要复写该方法)
那么何时需要复写OnMeansure()呢?
当我们设置明确的宽度和高度时,系统帮我们测量的结果就是我们设置的结果,当我们设置为WRAP_CONTENT,或者MATCH_PARENT,系统帮我们测量的结果就是MATCH_PARENT的长度(系统帮我们测量的高度和宽度都是MATCH_PARNET)。所以,当设置了WRAP_CONTENT时,我们需要自己进行测量,即重写onMesure方法”:重写之前先了解MeasureSpec的specMode,一共三种类型:
EXACTLY:一般是设置了明确的值或者是MATCH_PARENT
AT_MOST:表示子布局限制在一个最大值内,一般为WARP_CONTENT
UNSPECIFIED:表示子布局想要多大就多大,很少使用
4、在自定义View类中重写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); }
为了区别于TextView以突显自定义View,我们可以添加一个setOnListener()
0 0
- [Android自定义控件] Android自定义控件
- Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- Android 自定义控件 单页翻书控件
- android虚线控件---自定义控件
- android自定义控件实例 --控件
- Android自定义控件--组合控件
- android 控件 自定义组合控件
- 【android自定义控件】ProgressBar自定义
- Android自定义控件 自定义属性
- Android自定义控件 -- 自定义View
- Android自定义控件 -- 自定义ViewGroup
- Android 自定义控件 自定义标题栏
- Android 自定义控件-自定义进度条。
- hpuoj 1714: 感恩节KK专场——送给新生的礼物 【大水】【校赛】
- javaScript中数据类型,$ is not a function问题
- 11月—快乐英语
- 代理登录,token,ticket
- 返回结果集的存储过程实例及调用
- Android 自定义控件
- php+mysql
- 【Educational Codeforces Round 2C】【贪心】最少修改数下得到字典序尽可能小回文串
- openstack之neutron代码分析---(1)neutron初始化流程
- 转载:linux 内存管理——内核的shmall 和shmmax 参数
- Remove Duplicates from Sorted Array II
- 今天来打打java大数模板(整型)
- Android UICC 好文关键字备忘
- 【C语言提高01】冒泡排序和数组做参数的问题