如何在android中设置自定义视图组的边距

时间:2014-12-05 11:32:33

标签: java android viewgroup

我已经定义了一个从ViewGroup扩展的自定义视图组类,我想通过此视图组的java代码设置边距。但是当我使用ViewGroup.MarginLayoutParams时,我有以下错误:

12-05 14:58:58.593: E/AndroidRuntime(32277): FATAL EXCEPTION: main
12-05 14:58:58.593: E/AndroidRuntime(32277): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sample/com.example.sample.SampleActivity}: java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
12-05 14:58:58.593: E/AndroidRuntime(32277):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1829)
12-05 14:58:58.593: E/AndroidRuntime(32277):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1848)
12-05 14:58:58.593: E/AndroidRuntime(32277):    at android.app.ActivityThread.access$1500(ActivityThread.java:177)
12-05 14:58:58.593: E/AndroidRuntime(32277):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1041)
12-05 14:58:58.593: E/AndroidRuntime(32277):    at android.os.Handler.dispatchMessage(Handler.java:130)
12-05 14:58:58.593: E/AndroidRuntime(32277):    at android.os.Looper.loop(Looper.java:384)
12-05 14:58:58.593: E/AndroidRuntime(32277):    at android.app.ActivityThread.main(ActivityThread.java:3975)
12-05 14:58:58.593: E/AndroidRuntime(32277):    at java.lang.reflect.Method.invokeNative(Native Method)
12-05 14:58:58.593: E/AndroidRuntime(32277):    at java.lang.reflect.Method.invoke(Method.java:538)
12-05 14:58:58.593: E/AndroidRuntime(32277):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
12-05 14:58:58.593: E/AndroidRuntime(32277):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:732)
12-05 14:58:58.593: E/AndroidRuntime(32277):    at dalvik.system.NativeStart.main(Native Method)
12-05 14:58:58.593: E/AndroidRuntime(32277): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
12-05 14:58:58.593: E/AndroidRuntime(32277):    at com.example.pager.HorizontalPager.getLayoutParams(HorizontalPager.java:212)
12-05 14:58:58.593: E/AndroidRuntime(32277):    at com.example.sample.SampleActivity.onCreate(SampleActivity.java:95)
12-05 14:58:58.593: E/AndroidRuntime(32277):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1149)
12-05 14:58:58.593: E/AndroidRuntime(32277):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1793)
12-05 14:58:58.593: E/AndroidRuntime(32277):    ... 11 more

我该怎么办?提前致谢

1 个答案:

答案 0 :(得分:0)

我将我的视图组放入relativelayout并且它可以正常工作。