我知道很多人都遇到过类似的问题,但我找不到解决方案。我创建了一个扩展 SurfaceView 的自定义视图。当我使用时,此视图非常有效:
setContentView(graphView);
但是我希望它嵌入到 HorizontalScrollView 中。如果我用 xml 尝试这个:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<com.stuff.graph.GraphView
android:id="@+id/graph"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</HorizontalScrollView>
</FrameLayout>
然后我得到一个致命的异常,我的应用程序关闭。
如果我尝试以编程方式解决此问题:
graphView = new GraphView(this);
graphView.setVisibility(View.VISIBLE);
myScrollView = new HorizontalScrollView(this);
graphView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
myScrollView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
myScrollView.addView(graphView);
setContentView(myScrollView);
然后屏幕就黑了。我还尝试了上面显示的代码的许多变体,但没有一个成功。我哪里错了?