tiwenzhuanyong
tiwenzhuanyong
2013-04-02 00:01
浏览 188

代码设置RelativeLayout宽度时报错?

[code="xml"]
<?xml version="1.0" encoding="utf-8"?>
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">
。。。。。

[/code]
代码中需要对RelativeLayout的宽度进行设定:
[code="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);
[/code]
但是报错:
[code="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)
[/code]
查了些资料,比如:http://blog.csdn.net/id19870510/article/details/6398049
说是需要设置父layout,但是试了后依然报错,请问到底如何更改?
谢谢

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • iteye_1194
    iteye_1194 2013-04-09 00:24
    已采纳

    relativeLayout 是LinearLayout的子控件~它的layoutparam应该是LinearLayout给他的所以应该是LinearLayout.LayoutParam ~~~~不知道你理解了没~~~如果relativeLayout 有子控件的话~那它的子控件用的的就是relativeLayout 给他们的RinearLayout.LayoutParam

    点赞 评论

相关推荐