这段代码正在扼杀我。我试图以编程方式设置LinearLayout
(包含子项)的宽度和高度,应用程序正在破坏。这是代码:
LinearLayout ll = (LinearLayout) findViewById(R.id.right_t);
ll.setLayoutParams(new android.widget.LinearLayout.LayoutParams(200, 200));
如您所见,我100%确定我可以使用正确的布局框架。然而,应用程序错误
1-22 19:54:13.135: ERROR/AndroidRuntime(4560): FATAL EXCEPTION: main
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1317)
at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1264)
at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:292)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:314)
at android.view.View.measure(View.java:12863)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12863)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
at android.view.View.measure(View.java:12863)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2256)
at android.view.View.measure(View.java:12863)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1197)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2585)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4507)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
带有子项的LinearLayout
设置在自己的XML文件中,并通过<include>
标记加载到主XML中。
有什么想法吗?当我没有这样做时,为什么我会说LinearLayout
到RelativeLayout
?
答案 0 :(得分:5)
LayoutParams的类型对应于您要在其中添加LinearLayout
的父布局的类型。所以我想在这种情况下,父布局是RelativeLayout
,你应该创建一个RelativeLayout.LayoutParams
而不是LinearLayout.LayoutParams
。