Android 的GraphView库有一个 GraphView 类,它是一个用于创建图形的 View。这个抽象类扩展了 LinearLayout,因此您可以通过嵌套 LinearLayouts 在布局中包含图形,如下所示:
xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mainLayout">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World!" />
<LinearLayout
android:id="@+id/subLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.java:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GraphViewData[] data = new GraphViewData[num];
double v = 0;
for (int i = 0; i < 10; i++) {
v += 0.1;
data[i] = new GraphViewData(i, Math.sin(v));
}
GraphViewSeries seriesSin = new GraphViewSeries("Sinus", null, data);
GraphView graphView = new LineGraphView(this, "DemoGraphView");
graphView.addSeries(seriesSin);
graphView.setViewPort(2, 10);
LinearLayout layout = (LinearLayout) findViewById(R.id.subLayout);
layout.addView(graphView);
}
}
我现在嵌套 LinearLayouts 但也许有更好的方法可以在布局文件中包含图形,例如。就像您放置 TextView 或 Button 视图一样?