Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 自定义视图上下文是否始终是一个活动?_Android_Android Context - Fatal编程技术网

Android 自定义视图上下文是否始终是一个活动?

Android 自定义视图上下文是否始终是一个活动?,android,android-context,Android,Android Context,我有一个自定义视图,它将从xml中膨胀,并具有一些自定义xml属性 如果xml属性设置为true,此视图将在ActionBar中设置一些内容。 因此,我需要一个活动操作栏的参考 我的问题是: 我是否可以假定在的构造函数中传递了上下文 class MyView extends View { public MyView(Context context, AttributeSet attrs, int defStyle){ Activity a = (Activity) c

我有一个自定义视图,它将从xml中膨胀,并具有一些自定义xml属性

如果xml属性设置为true,此视图将在ActionBar中设置一些内容。 因此,我需要一个活动操作栏的参考

我的问题是: 我是否可以假定在的构造函数中传递了上下文

class MyView extends View {
    public MyView(Context context, AttributeSet attrs, int defStyle){

         Activity a = (Activity) context;

    }
}
我已经用不同的设备和不同的安卓版本测试了这一点,似乎上下文是一种活动


有人知道这一点吗?

没有。如果你对包含视图的布局进行主题化,你将得到一个ContextThemeWrapper。

是的,这是一个活动上下文。如果我从布局更扁平的程序(如getSystemServiceContext.layout\u INFLATER\u SERVICE;)中膨胀我的自定义视图,也会如此?为此,您需要一个活动上下文。