//rl_pager 是RelativeLayout findViewById(R.id.rl_pager).setLayoutParams(new RelativeLayout.LayoutParams(width, width));
当我这么设置layoutparams的时候报错了,原因:
setLayoutParams(params) 这个params 是父布局的 layout,并非自身的layout。。。。
因为我这里 rl_pager的父节点是 linearlayout 所以 我这里正确的应该是:
//rl_pager 是RelativeLayout findViewById(R.id.rl_pager).setLayoutParams(new LinearLayout.LayoutParams(width, width));
下面一段对话,参考下吧:
-
代码设置RelativeLayout宽度时报错?0
Xml代码
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/linearLayout_parent"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center_horizontal"
- android:orientation="vertical" >
- <RelativeLayout
- android:id="@+id/relativeLayout1"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- 。。。。。
代码中需要对RelativeLayout的宽度进行设定:
Java代码
- RelativeLayout relativeLayout = (RelativeLayout)this.findViewById(R.id.relativeLayout1);
- RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(cell_Width, LayoutParams.MATCH_PARENT);
- param.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
- relativeLayout.setLayoutParams(param);
但是报错:
Java代码
- 04-01 23:49:13.572: E/AndroidRuntime(1246): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
- 04-01 23:49:13.572: E/AndroidRuntime(1246): at android.widget.LinearLayout.measureVertical(LinearLayout.java:587)
- 04-01 23:49:13.572: E/AndroidRuntime(1246): at android.widget.LinearLayout.onMeasure(LinearLayout.java:519)
- 04-01 23:49:13.572: E/AndroidRuntime(1246): at android.view.View.measure(View.java:10828)
- 04-01 23:49:13.572: E/AndroidRuntime(1246): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
- 04-01 23:49:13.572: E/AndroidRuntime(1246): at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
- 04-01 23:49:13.572: E/AndroidRuntime(1246): at android.view.View.measure(View.java:10828)
- 04-01 23:49:13.572: E/AndroidRuntime(1246): at android.widget.LinearLayout.measureVertical(LinearLayout.java:764)
- 04-01 23:49:13.572: E/AndroidRuntime(1246): at android.widget.LinearLayout.onMeasure(LinearLayout.java:519)
- 04-01 23:49:13.572: E/AndroidRuntime(1246): at android.view.View.measure(View.java:10828)
- 04-01 23:49:13.572: E/AndroidRuntime(1246): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
- 04-01 23:49:13.572: E/AndroidRuntime(1246): at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
- 04-01 23:49:13.572: E/AndroidRuntime(1246): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:1889)
- 04-01 23:49:13.572: E/AndroidRuntime(1246): at android.view.View.measure(View.java:10828)
- 04-01 23:49:13.572: E/AndroidRuntime(1246): at android.view.ViewRoot.performTraversals(ViewRoot.java:938)
- 04-01 23:49:13.572: E/AndroidRuntime(1246): at android.view.ViewRoot.handleMessage(ViewRoot.java:2040)
- 04-01 23:49:13.572: E/AndroidRuntime(1246): at android.os.Handler.dispatchMessage(Handler.java:99)
- 04-01 23:49:13.572: E/AndroidRuntime(1246): at android.os.Looper.loop(Looper.java:132)
- 04-01 23:49:13.572: E/AndroidRuntime(1246): at android.app.ActivityThread.main(ActivityThread.java:4123)
- 04-01 23:49:13.572: E/AndroidRuntime(1246): at java.lang.reflect.Method.invokeNative(Native Method)
- 04-01 23:49:13.572: E/AndroidRuntime(1246): at java.lang.reflect.Method.invoke(Method.java:491)
- 04-01 23:49:13.572: E/AndroidRuntime(1246): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
- 04-01 23:49:13.572: E/AndroidRuntime(1246): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
- 04-01 23:49:13.572: E/AndroidRuntime(1246): at dalvik.system.NativeStart.main(Native Method)
查了些资料,比如:http://blog.csdn.net/id19870510/article/details/6398049
说是需要设置父layout,但是试了后依然报错,请问到底如何更改?
谢谢2013年4月02日 00:01
tiwenzhuanyong
0
0 0 4
1个答案按时间排序按投票排序
00
-
采纳的答案
relativeLayout 是LinearLayout的子控件~它的layoutparam应该是LinearLayout给他的所以应该是LinearLayout.LayoutParam ~~~~不知道你理解了没~~~如果relativeLayout 有子控件的话~那它的子控件用的的就是relativeLayout 给他们的RinearLayout.LayoutParam
时间: 2024-10-14 01:33:32