自定义View三个方法的意义
2017-02-14 22:55
232 查看
自定义 View 是每个Android 都经常接触的,说来惭愧,我到现在对它的三个构造方法还是一知半解,平时只 copy,接错,现在好好补补这些基础知识
很多时候系统自带的View满足不了设计的要求,就需要自定义View控件。
自定义View首先要实现一个继承自View的类。添加类的构造方法,override父类的方法,如onDraw,(onMeasure)等。如果自定义的View有自己的属性,需要在values下建立attrs.xml文件,在其中定义属性,同时代码也要做修改。
这样一个简单的自定义View就可以使用了。可以改变一下背景颜色,在MyView类中添加
values/attrs.xml 这个是自定义属性的文件
完成上面的两步之后就可以在代码中实例化这个布局文件了
自定义属性代码中示例:
官方解释
Constructor that is called when inflating a view from XML. This is called when a view is being constructed from an XML file, supplying attributes that were specified in the XML file. This version uses a default style of 0, so the only attribute values applied are those in the Context’s Theme and the given AttributeSet
大致应该是这个方法是通过xml文件来创建一个view对象的时候调用。很显然xml文件一般是布局文件,就是现实控件的时候调用,而布局文件中免不了会有属性的设置,如android:layout_width等,对这些属性的设置对应的处理代码也在这个方法中完成
Perform inflation from XML and apply a class-specific base style. This constructor of View allows subclasses to use their own base style when they are inflating. For example, a Button class’s constructor would call this version of the super class constructor and supply R.attr.buttonStyle fordefStyle; this allows the theme’s button style to modify all of the base view attributes (in particular its background) as well as the Button class’s attributes.
第四个参数要求最低 API 21,不是很清楚什么意思,没用过
很多时候系统自带的View满足不了设计的要求,就需要自定义View控件。
自定义View首先要实现一个继承自View的类。添加类的构造方法,override父类的方法,如onDraw,(onMeasure)等。如果自定义的View有自己的属性,需要在values下建立attrs.xml文件,在其中定义属性,同时代码也要做修改。
1. 一个参数
第一属于程序内实例化时采用,之传入Context即可public class MyView extends TextView{ public MyView(Context context) { super(context); // TODO Auto-generated constructor stub } } public class NewView extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_newview); setContentView(new MyView(this)); } }
这样一个简单的自定义View就可以使用了。可以改变一下背景颜色,在MyView类中添加
protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); canvas.drawColor(Color.BLUE); }
2. 两个参数
第二个用于layout文件实例化,会把XML内的参数通过AttributeSet带入到View内public MyView(Context context,AttributeSet attrs){ super(context, attrs); } //命名空间,有了它,才能用自定义属性 xmlns:test="http://schemas.android.com/apk/res-auto" <com.lizi.newset.customview.attrs.MyView android:id="@+id/myView" android:layout_height="match_parent" android:layout_width="wrap_content" test:textSize="50px" test:textColor="#ff00ff"/> />
values/attrs.xml 这个是自定义属性的文件
<declare-styleable name="MyView"> <attr name="textColor" format="color" /> <attr name="textSize" format="dimension" /> </declare-styleable>
完成上面的两步之后就可以在代码中实例化这个布局文件了
setContentView(new MyView(this));
自定义属性代码中示例:
public MyView(Context context, AttributeSet attrs) { super(context, attrs); mPaint = new Paint(); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyView); int textColor = a.getColor(R.styleable.MyView_textColor, Color.BLACK); float textSize = a.getDimension(R.styleable.MyView_textSize, 36); mPaint.setTextSize(textSize); mPaint.setColor(textColor); a.recycle(); }
官方解释
Constructor that is called when inflating a view from XML. This is called when a view is being constructed from an XML file, supplying attributes that were specified in the XML file. This version uses a default style of 0, so the only attribute values applied are those in the Context’s Theme and the given AttributeSet
大致应该是这个方法是通过xml文件来创建一个view对象的时候调用。很显然xml文件一般是布局文件,就是现实控件的时候调用,而布局文件中免不了会有属性的设置,如android:layout_width等,对这些属性的设置对应的处理代码也在这个方法中完成
3. 三个参数
第三个主题的style信息,也会从XML里带入public View (Context context, AttributeSet attrs,int defStyle)
Perform inflation from XML and apply a class-specific base style. This constructor of View allows subclasses to use their own base style when they are inflating. For example, a Button class’s constructor would call this version of the super class constructor and supply R.attr.buttonStyle fordefStyle; this allows the theme’s button style to modify all of the base view attributes (in particular its background) as well as the Button class’s attributes.
第四个参数要求最低 API 21,不是很清楚什么意思,没用过
相关文章推荐
- 自定义View获取dimen值得三个方法:getDimension,getDimensionPixelOffset和getDimensionPixelSize
- 自定义View的三个构造方法
- 自定义View获取dimen值得三个方法:getDimension,getDimensionPixelOffset和getDimensionPixelSize
- View的常用方法,以及最重要的三个方法。
- 关于自定义UIAlertView背景的方法收集
- Android自定义View研究(三) -- 获得Bitmap的三种方法
- Android自定义View研究(三) -- 获得Bitmap的三种方法
- Android中的自定义Adapter(继承自BaseAdapter)——与系统Adapter的调用方法一致——含ViewHolder显示效率的优化
- Android UI 之居中绘制文本内容的正确方法——实现自定义一个TextView
- 移植自定义View过程中遇到的问题及解决方法
- Android中自定义View的研究(三) -- 获得Bitmap的三种方法
- Android中自定义View的研究(三) -- 获得Bitmap的三种方法
- 重写PageStatePersister属性 自定义压缩方法来减小ViewState大小来优化页面加载时间
- 自定义View的onMeasure方法理解
- Android 自定义view 和 onMeasure方法介绍
- android中自定义view常用方法
- 自定义View的常用方法
- Android添加自定义View的方法
- 关于自定义AlertView背景的方法收集
- 解析在Android中为TextView增加自定义HTML标签的实现方法