资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Android中怎么自定义xml属性

本篇文章给大家分享的是有关Android中怎么自定义xml属性,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名申请、网络空间、营销软件、网站建设、肇东网站维护、网站推广。

1. 首先创建一个新的android application.

2. 创建属性
在res/values/ 下创建一个attr.xml 文件,定义好需要的attributes

                                                     

3. 创建自定义的View

创建一个View, CustomView 继承自View(根据具体的情况,如果需求和已经存在的widget或者layout相差不大,就继承,重写一些方法)

package com.hualu.androidview;   import android.content.Context;   import android.content.res.TypedArray;   import android.graphics.Canvas;   import android.graphics.Paint;   import android.util.AttributeSet;   import android.view.View;   public class CustomView extends View {       private  Paint p = null;       private String text =  null;       public CustomView(Context context) {           super(context);           initCustomView() ;       }       public CustomView(Context context, AttributeSet attrs){           super(context, attrs ) ;           initCustomView() ;           TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.custom) ;           int indexCount = a.getIndexCount() ;           for(int i = 0 ; i < indexCount ; i ++){               int index = a.getIndex(i) ;               switch (index) {               case R.styleable.custom_text:                   text = a.getString(index) ;                   break;               case R.styleable.custom_size:                   p.setTextSize(a.getInt(index, 0));                    break;               case R.styleable.custom_color:                   p.setColor(a.getColor(index, 0xFF000000)) ;                   break;               }           }           a.recycle() ;       }      void initCustomView(){            p = new Paint();            p.setAntiAlias(true);       } ;       @Override       protected void onDraw(Canvas canvas) {           super.onDraw(canvas);           canvas.drawText(text, 10, 10, p) ;       }   }

4. 在layout的文件使用自定义的view

                 

以上就是Android中怎么自定义xml属性,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


文章标题:Android中怎么自定义xml属性
本文链接:http://cdkjz.cn/article/jecjcj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220