代码设置RelativeLayout宽度时报错?
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,但是试了后依然报错,请问到底如何更改?
采纳的答案
relativeLayout 是LinearLayout的子控件~它的layoutparam应该是LinearLayout给他的所以应该是LinearLayout.LayoutParam ~~~~如果relativeLayout 有子控件的话~那它的子控件用的的就是relativeLayout ,给他们的RinearLayout.LayoutParam.
另:
我们经常会动态修改RelativeLayout的宽高,这样的代码,比较简单,就是修改Relativelayout的LayoutParams就可以.代码一般如下:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
RelativeLayout ss = (RelativeLayout) findViewById(R.id.myRelativeLayout);
ss.setLayoutParams(new RelativeLayout.LayoutParams(width, height));
这种修改,在大多数情况下,没有问题,而且很好用,可是我遇到的问题是这样,就是我要修改的RelativeLayout,整个布局的中间的一个部分.
这里我要修改的是中间绿色的 生活这个框,如果我们用上面的方法,你就会发现RelativeLayout的高度确实被改了,可是他却改变了整体布局,把最上面一行工具栏给整没了.如下
这个可不是我们要的啊.这个怎么改呢.其实,你仔细看一下代码,就能明白了,上面的方法,他new了一个LayoutParams,问题就在这里,正确的方法是,先获取—> 修改—>再设置
代码如下:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
RelativeLayout.LayoutParams linearParams = (RelativeLayout.LayoutParams)rela_addnote_notetype.getLayoutParams();
linearParams.height = 44;
rela_addnote_notetype.setLayoutParams(linearParams);
这回再试下,如图