我创建了自己的View扩展来显示自定义图形,但似乎遇到了显示我的自定义类的多个实例的问题。当我运行程序时,我没有错误,但只在我试图显示两个时显示一个图表。我假设我在扩展View或我的html布局方法时遇到了一些错误。
我的GraphView.java类:
package com.xxx.yyy; import View; ... public class GraphView extends View{ public GraphView(Context context, AttributeSet set) { super(context, set); init(); } public GraphView(Context context) { super(context); init(); } public void init(){ ... //set display constants, axis values, and Paint objects } public void setData(long[] passX, float[] passY){ ... //get data to graph } @Override protected void onDraw(Canvas passCanvas){ ... //draw's the graph onto the view }//onDraw }
main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.xxx.yyy.GraphView
android:id="@+id/x_0"
android:layout_width = "fill_parent"
android:layout_height="wrap_content"/>
<com.xxx.yyy.GraphView
android:id="@+id/y_0"
android:layout_width = "fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/x_0"/>
<TextView
android:id="@+id/text1"
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/y_0"
android:text="testing 123"/>
</RelativeLayout>
和我的活动:
package com.xxx.yyy; import android.app.Activity; ... import android.widget.TextView; public class AccelDataStats extends Activity{ private GetAccelData mApp; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.Main); mApp = (GetAccelData) GetAccelData.getContext(); //gets context from previous activity GraphView x_0 = (GraphView) findViewById(R.id.x_0); x_0.setData(mApp.t, mApp.x[0]); x_0.setTitle("x_0 testing"); GraphView y_0 = (GraphView) findViewById(R.id.y_0); y_0.setData(mApp.t, mApp.y[0]); y_0.setTitle("y_0 123"); } }
如何显示我的GraphView.java的多个实例?非常感谢你的帮助。