它的构造函数被调用的看法? [英] which constructor to be called for view?

查看:124
本文介绍了它的构造函数被调用的看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从查看我的自定义视图扩展。有3种观点构造函数:

I have my custom view extended from View. There are 3 view constructors:

  1. 视图(上下文的背景下,ATTRS的AttributeSet,INT defStyle)
  2. 视图(上下文的背景下,ATTRS的AttributeSet)
  3. 视图(上下文的背景下)
  1. View(Context context, AttributeSet attrs, int defStyle)
  2. View(Context context, AttributeSet attrs)
  3. View(Context context)

这是我的活动我称之为 std.setContentView(R.layout.main)。第二个构造函数获取调用在我看来。为什么第二个?如何提前知道哪些人会被调用,为什么?

From my activity I call std.setContentView(R.layout.main). The second constructor is getting called in my view. Why the second one? How to know in advance which one will be called and why?

推荐答案

从Android开发者网站下的文档查看:

From the Android developer site under documentation for View:

<一个href="http://developer.android.com/reference/android/view/View.html#View%28android.content.Context%29"><$c$c>public查看(上下文的背景下)

简单构造函数来创建从code视图时使用。

Simple constructor to use when creating a view from code.

所以该构造是,你可以用什么来在Java中创建一个视图。当你从XML夸大它不会被调用。

So this constructor is what you can use to create a View in Java. It will not be called when you inflate from XML.

<一个href="http://developer.android.com/reference/android/view/View.html#View%28android.content.Context,%20android.util.AttributeSet%29"><$c$c>public查看(上下文的背景下,ATTRS的AttributeSet)

构造充气从XML的视图时调用。当一个视图被从一个XML文件构成,提供在XML文件中所指定的属性,这就是所谓的。此版本使用默认值为0的风格,所以应用的唯一属性值是在语境中的主题和给定的AttributeSet。

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.

所有的孩子都被加入后的方法onFinishInflate()将被调用。

The method onFinishInflate() will be called after all children have been added.

所以,当你从充气XML视图,当你不指定样式此构造函数将被调用。

So this constructor will be called when you inflate a View from XML when you don't specify a style.

<一个href="http://developer.android.com/reference/android/view/View.html#View%28android.content.Context,%20android.util.AttributeSet,%20int%29"><$c$c>public查看(上下文的背景下,ATTRS的AttributeSet,INT defStyle)

从XML执行通货膨胀和应用类特定的基本样式。鉴于此构造函数允许子类用自己的基本样式,当它们被夸大。例如,一个Button类的构造函数调用此版本的父类的构造函数和供应R.attr.buttonStyle为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 for defStyle; 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.

您应该实现所有这些构造函数,但是你可以通过调用这(背景下,空)这(背景下,ATTRS,0)前两个,分别为。

You should implement all of these constructors, but you can put all of the work in the third one by calling this(context, null) and this(context, attrs, 0) for the first two, respectively.

这篇关于它的构造函数被调用的看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆