android 5.0.2 上未调用自定义视图构造函数
Custom view constructor does not get called on android 5.0.2
我创建了自定义视图:
public class SomeView extends View
自定义视图构造函数:
public SomeView (Context context)
{
super(context);
}
// Called when view is inflated from xml
public SomeView (Context context, AttributeSet attrs)
{
super(context, attrs);
}
// Perform inflation from XML and apply a class-specific base style from a theme attribute.
public SomeView (Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
我还尝试了 api 21 的第 4 个构造函数,但没有成功:
public VeediView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{
super(context, attrs,defStyleAttr, defStyleRes);
}
在 xml 布局中,我正在定义此视图并且一切正常。
在 Galaxy S2 上测试工作正常并且调用了视图构造函数但是当 运行 Nexus-7 android 5.0.2 上的应用程序时根本不会调用构造函数。
知道为什么吗?
会不会和root设备有关?
相关xml视图:
<com.package.name
android:id="@+id/scene"
android:onClick="startx"
style="@style/txt_money_style"
android:layout_width="72dp"
android:layout_height="72dp"
android:background="@drawable/wtbtn"
android:layout_gravity="right"
android:gravity="center_vertical|right"
/>
在 API 21 中现在有一个 4th constructor 可能是您的 XML 正在调用它。
来自文档:
public View (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
Added in API level 21
Perform inflation from XML and apply a class-specific base style from a theme attribute or style resource. This constructor of View allows subclasses to use their own base style when they are inflating.
When determining the final value of a particular attribute, there are four inputs that come into play:
- Any attribute values in the given AttributeSet.
- The style resource specified in the AttributeSet (named "style").
- The default style specified by defStyleAttr.
- The default style specified by defStyleRes.
- The base values in this theme.
Each of these inputs is considered in-order, with the first listed taking precedence over the following ones. In other words, if in the AttributeSet you have supplied , then the button's text will always be black, regardless of what is specified in any of the styles.
Parameters
context The Context the view is running in, through which it can access the current theme, resources, etc.
attrs The attributes of the XML tag that is inflating the view.
defStyleAttr An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for defaults.
defStyleRes A resource identifier of a style resource that supplies default values for the view, used only if defStyleAttr is 0 or can not be found in the theme. Can be 0 to not look for defaults.
我认为你最好使用这个构造函数:
public SomeView (Context context)
{
this(context , null);
}
// Called when view is inflated from xml
public SomeView (Context context, AttributeSet attrs)
{
this(context, attrs , 0);
}
// Perform inflation from XML and apply a class-specific base style from a theme attribute.
public SomeView (Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
// Initialize customize constructor here
}
这里是引用的源代码View.java class。如果你检查一下,你会发现 public View(Context context)
总是被调用。如果您认为它没有被调用但您看到了视图,那么问题是 检测它是否被调用 的部分,而不是 Android 代码。你应该进去看看它可能是在 AS 中记录代码或一些错误的过滤器,或类似的。
从源码也可以看出,这是新的构造函数,在Android5.0及更高版本中使用,实现最多。
public View(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
问题是我得到了这段代码,但并不是我自己开发的,在尝试了所有方法后发现该应用程序有多个布局文件:
大布局、小布局等...
我只在布局文件夹上定义了自定义视图,因此切换到其他屏幕尺寸会调用常规视图。
我想其他人可以从我的错误中吸取教训,我希望 Android Studio 或 Eclipse 可以支持某种 setContentView(R.layout.activity_scene) 和相关的文件调试选项
所以答案是确保所有布局都定义了自定义视图
我创建了自定义视图:
public class SomeView extends View
自定义视图构造函数:
public SomeView (Context context)
{
super(context);
}
// Called when view is inflated from xml
public SomeView (Context context, AttributeSet attrs)
{
super(context, attrs);
}
// Perform inflation from XML and apply a class-specific base style from a theme attribute.
public SomeView (Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
我还尝试了 api 21 的第 4 个构造函数,但没有成功:
public VeediView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{
super(context, attrs,defStyleAttr, defStyleRes);
}
在 xml 布局中,我正在定义此视图并且一切正常。
在 Galaxy S2 上测试工作正常并且调用了视图构造函数但是当 运行 Nexus-7 android 5.0.2 上的应用程序时根本不会调用构造函数。
知道为什么吗?
会不会和root设备有关?
相关xml视图:
<com.package.name
android:id="@+id/scene"
android:onClick="startx"
style="@style/txt_money_style"
android:layout_width="72dp"
android:layout_height="72dp"
android:background="@drawable/wtbtn"
android:layout_gravity="right"
android:gravity="center_vertical|right"
/>
在 API 21 中现在有一个 4th constructor 可能是您的 XML 正在调用它。
来自文档:
public View (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
Added in API level 21
Perform inflation from XML and apply a class-specific base style from a theme attribute or style resource. This constructor of View allows subclasses to use their own base style when they are inflating.
When determining the final value of a particular attribute, there are four inputs that come into play:
- Any attribute values in the given AttributeSet.
- The style resource specified in the AttributeSet (named "style").
- The default style specified by defStyleAttr.
- The default style specified by defStyleRes.
- The base values in this theme.
Each of these inputs is considered in-order, with the first listed taking precedence over the following ones. In other words, if in the AttributeSet you have supplied , then the button's text will always be black, regardless of what is specified in any of the styles.
Parameters
context The Context the view is running in, through which it can access the current theme, resources, etc. attrs The attributes of the XML tag that is inflating the view. defStyleAttr An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for defaults. defStyleRes A resource identifier of a style resource that supplies default values for the view, used only if defStyleAttr is 0 or can not be found in the theme. Can be 0 to not look for defaults.
我认为你最好使用这个构造函数:
public SomeView (Context context)
{
this(context , null);
}
// Called when view is inflated from xml
public SomeView (Context context, AttributeSet attrs)
{
this(context, attrs , 0);
}
// Perform inflation from XML and apply a class-specific base style from a theme attribute.
public SomeView (Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
// Initialize customize constructor here
}
这里是引用的源代码View.java class。如果你检查一下,你会发现 public View(Context context)
总是被调用。如果您认为它没有被调用但您看到了视图,那么问题是 检测它是否被调用 的部分,而不是 Android 代码。你应该进去看看它可能是在 AS 中记录代码或一些错误的过滤器,或类似的。
从源码也可以看出,这是新的构造函数,在Android5.0及更高版本中使用,实现最多。
public View(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
问题是我得到了这段代码,但并不是我自己开发的,在尝试了所有方法后发现该应用程序有多个布局文件:
大布局、小布局等...
我只在布局文件夹上定义了自定义视图,因此切换到其他屏幕尺寸会调用常规视图。
我想其他人可以从我的错误中吸取教训,我希望 Android Studio 或 Eclipse 可以支持某种 setContentView(R.layout.activity_scene) 和相关的文件调试选项
所以答案是确保所有布局都定义了自定义视图