Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.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 - Fatal编程技术网

Android 自定义视图和主活动

Android 自定义视图和主活动,android,Android,我想在自定义视图中使用不同类中的点阵列。但该点数组实际上是在主活动中声明的。自定义视图只扩展视图,所以当我在ondraw中使用点数组时,它是未声明的 实际上,我希望在ondraw函数中的数组中使用坐标,然后在我的主要活动中使用它们,我的其余代码就驻留在这里。然后我还想在那个视图上添加一个按钮 问题2。我使用了另一种方法,我没有为自定义视图创建新的java文件,并尝试在main.xml中添加两个线性布局,一个用于按钮,另一个用于自定义视图,但在这里,当我加载主活动中的视图时,它不会加载语法 <

我想在自定义视图中使用不同类中的点阵列。但该点数组实际上是在主活动中声明的。自定义视图只扩展视图,所以当我在ondraw中使用点数组时,它是未声明的

实际上,我希望在ondraw函数中的数组中使用坐标,然后在我的主要活动中使用它们,我的其余代码就驻留在这里。然后我还想在那个视图上添加一个按钮

问题2。我使用了另一种方法,我没有为自定义视图创建新的java文件,并尝试在main.xml中添加两个线性布局,一个用于按钮,另一个用于自定义视图,但在这里,当我加载主活动中的视图时,它不会加载语法

<LinearLayout android:orientation="horizontal"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content">

    <Button android:text="Button" android:id="@+id/btn1"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" />
</LinearLayout>

<LinearLayout android:orientation="horizontal"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content">

    <com.faisal.FaisalActivity.DemoView
        android:id="@+id/demoView1" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="@string/hello" />

    <com.faisal.FaisalActivity.DemoView
        android:id="@+id/demoView1" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>

上面是我的线性布局忽略我刚刚删除的括号强文本你可以在自定义视图中创建setData方法,在xml中创建自定义视图的id

在activity类的onCreate函数中,可以使用自定义视图的FindViewByDid并将数据设置为对象。通过这种方式,您可以将数据从活动传递到视图

这是我创建的一个示例,这里我将点数组传递给作为自定义视图的线形图视图