如何在ScrollView中更改布局的大小?

时间:2011-07-24 21:41:03

标签: java android

我有一个嵌套在ScrollView和Horizo​​ntalScrollView中的自定义View,如下所示:

<HorizontalScrollView>
    <ScrollView>
        <GameView>
        </GameView>
    </ScrollView>
</HorizontalScrollView>

我想根据用户操作(放大或缩小)更改GameView大小但是当我尝试时

gameView.setLayoutParams(new LayoutParams((int)(gameView.BOARD_WIDTH * gameView.STEP), (int)(gameView.BOARD_HEIGHT * gameView.STEP)));

我得到一个例外:

java.lang.ClassCastException: android.view.ViewGroup$LayoutParams

at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1066)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.widget.ScrollView.onMeasure(ScrollView.java:296)
at android.view.View.measure(View.java:8313)
android.widget.HorizontalScrollView.measureChildWithMargins(HorizontalScrollView.java:1075)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.widget.HorizontalScrollView.onMeasure(HorizontalScrollView.java:302)
at android.view.View.measure(View.java:8313)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
at android.view.View.measure(View.java:8313)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.view.View.measure(View.java:8313)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.view.View.measure(View.java:8313)
at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)

有没有办法改变游戏视图的大小?

编辑:我已经解决了这个问题,anwser是:

gameView.setLayoutParams(new ScrollView.LayoutParams((int)(BOARD_WIDTH * STEP), (int)(BOARD_HEIGHT * STEP)));

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题,解决方案是使用父视图的LayoutParams:

gameView.setLayoutParams(new ScrollView.LayoutParams((int)(BOARD_WIDTH * STEP), (int)(BOARD_HEIGHT * STEP)));