tiwenzhuanyong 2013-04-02 00:01
浏览 198
已采纳

代码设置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 2013-04-09 00:24
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化