我知道很多人都遇到过类似的问题,但我找不到解决方案。我创建了一个扩展 SurfaceView 的自定义视图。当我使用以下命令时,此视图效果非常好:

setContentView(graphView);

不过我希望它嵌入到 Horizo​​ntalScrollView 中。如果我用 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);

然后屏幕就黑了。我还尝试了上面显示的代码的许多变体,但没有成功。我哪里错了?

有帮助吗?

解决方案 2

事实证明这并不是解决这个问题的真正方法:

http://www.mail-archive.com/android-developers@googlegroups.com/msg45804.html

我的解决方案是,我不会使用 SurfaceView,而是可能会绘制位图并将其放入图像视图中,然后将其放入 ScrollView 中。

其他提示

您应该能够在 xml 中获取它,没有问题 - 尝试将自定义图形视图嵌入到滚动视图内的 ListView 中;根据我使用horizo​​ntalScrollView的经验,他们往往对内容非常挑剔。如果这不起作用,请尝试测试框架视图是否存在问题。只需向水平滚动视图添加一个按钮,看看是否可以显示该按钮。如果没有,则尝试显示框架时出现问题,并且与您的自定义代码无关。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top