RelativeLayout title_bg = (RelativeLayout)FTU_Bluetooth.this.findViewById(R.id.titlebar);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 0x55);
title_bg.setLayoutParams(params);

如果用RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(int, int),则会报Exception: java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams

这是因为在这里的RelativeLayout在xml里是套在LinearLayout里的,必须建立父控件的LayoutParams。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
> <RelativeLayout
android:id="@+id/titlebar"
android:background="@drawable/main_title"
android:layout_width="fill_parent"
android:layout_height="66dp"
>

--------------------------------------------------------------------------

下面转帖壮志凌云的博客: http://sunjilife.blog.51cto.com/3430901/1159639

在android中用代码动态添加组件或者改变某种布局(组件)的高度时,会遇到如题所示的类转换异常。

These supply parameters to the parent of this view specifying how it should be arranged. There are many subclasses of ViewGroup.LayoutParams, and these correspond to the different subclasses of ViewGroup that are responsible for arranging their children.

So basically, if you are adding a view to another, you MUST set the LayoutParams of the view to the LayoutParams type that the parent uses, or you will get a runtime error.

如果你要将一个view添加到另一个布局中或者为这个view重新设定宽高等布局属性,你为该View设置的布局参数类型与其父类所使用的布局参数类型一样。此外就是说若是最上层的布局,则不需要设定此项。android中提供的布局参数类型目前一共12种:ViewPager.LayoutParams,LinearLayout.LayoutParams,RelativeLayout.LayoutParams,TableLayout.LayoutParams等等。关于

LayoutParams的说明http://www.cnblogs.com/shaweng/archive/2012/07/10/2585134.html

比如:

  1. <LinearLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content">
  5. <FrameLayout
  6. android:id="@+id/FrameLayout01"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content" />
  9. </LinearLayout>
  10. 若想在代码中动态改变FrameLayout的大小,应该这样写:
  11. FrameLayout frameLayout=(FrameLayout) convertView.findViewById(R.id.FrameLayout01);
  12. LinearLayout.LayoutParams ff=new LinearLayout.LayoutParams(LayoutParams.WRAP_C
  13. ONTENT, height);
  14. frameLayout.setLayoutParams(ff);

按照上面的说法,那么若是底层布局是LinearLayout,那么添加view的时候指定的宽高参数就必然是Linear.Layout

Params,可我在尝试过程中发现使用ViewGroup.LayoutParams,RelativeLayout.Params也可以运行,且不会出错,以下是代码:

  1. //test.xml 布局文件
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:id="@+id/test_root_linearlayout"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. android:orientation="vertical" >
  8. </LinearLayout>
  1. package com.example.aboutexpand;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.ViewGroup;
  5. import android.widget.LinearLayout;
  6. import android.widget.RelativeLayout;
  7. import android.widget.TextView;
  8. public class TestActivity extends Activity {
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. RelativeLayout rootLayout;
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.test);
  14. rootLayout = (LinearLayout) findViewById(R.id.test_root_linearlayout);
  15. LinearLayout.LayoutParams rootLinaerParams = new LinearLayout.LayoutParams(
  16. 100, 100);
  17. ViewGroup.LayoutParams rootGroupParams = new LinearLayout.LayoutParams(
  18. 100, 100);
  19. RelativeLayout.LayoutParams rootRelativeParams = new RelativeLayout.LayoutParams(
  20. 100, 100);
  21. TextView testView = new TextView(this);
  22. testView.setText("根布局测试动态添加组件并设置其大小");
  23. testView.setLayoutParams(rootGroupParams);
  24. rootLayout.addView(testView);
  25. // rootLayout.addView(testView, viewParams);
  26. }

经过试验,新增加的TextView的布局参数使用LinearLayout.LayoutParams,RelativeLayout.LayoutParams,ViewGroup.LayoutParams都是可以正确显示的,不相信的朋友,自己也可以试试看。至于这到底是什么原因呢,我目前也不清楚,希望有知道的朋友留言指教一下,谢谢

Exception: java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams的更多相关文章

  1. java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

    java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.w ...

  2. java.lang.ClassCastException android.widget.RelativeLayout LayoutParams 异常

    1.在xml布局文件如下所示: <RelativeLayout android:layout_width="match_parent" android:layout_heig ...

  3. Caused by: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams

    最近,在android中用代码动态改变某种布局(组件)的高度时,会遇到如题所示的类转换异常.上网查了一下,如下所示: These supply parameters to the parent of ...

  4. java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.L(转)

    09-09 10:19:59.979: E/AndroidRuntime(2767): FATAL EXCEPTION: main09-09 10:19:59.979: E/AndroidRuntim ...

  5. java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView

    最近在学习drawerLayout时,遇到这个bug.如下示: java.lang.ClassCastException: android.widget.RelativeLayout cannot b ...

  6. java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText

    Caused by: Java.lang.ClassCastException: Android.widget.TextView cannot be cast to android.widget.Ed ...

  7. 安卓出现错误: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText

    Caused by: Java.lang.ClassCastException: Android.widget.TextView cannot be cast to android.widget.Ed ...

  8. java.lang.ClassCastException: android.widget.ImageButton异常处理

    在调程序时总是出现异常关闭的现象,log显示: 03-26 07:58:09.528: E/AndroidRuntime(398): Caused by: java.lang.ClassCastExc ...

  9. java.lang.ClassCastException:android.widget.Button cannot be cast to android.widget.ImageView

    今天遇到一个错误也不知道怎么回事,上网搜了一下: 出现的问题是:java.lang.ClassCastException:android.widget.Button cannot be cast to ...

随机推荐

  1. LA 3644 - X-Plosives ( 也即UVA 1160)

    LA看题 请点击:传送门 UVA 上也有这题 :UVA 1160 - X-Plosives 题目大意就是如果车上存在 k 个简单化合物,正好包含 k 种元素 ,那么它们将有危险,此时你应该拒绝装车. ...

  2. OC学习篇之---归档和解挡

    今天我们来看一下OC中的一个重要知识点:归档 OC中的归档就是将对象写入到一个文件中,Java中的ObjectInputStream和ObjectOutputStream来进行操作的.当然在操作的这些 ...

  3. [java面试]宇信易诚 广州分公司 java笔试题目回顾录

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/28479895 作者:sushengmiyan -------------------- ...

  4. Oracle 11gR2 静默安装奇怪错误

    在静默安装Oracle 11gR2 的时候发现的奇怪错误,有点摸不着头脑 【步骤一】配置静默文件只安装软件 #--------------------------------------------- ...

  5. WIN32得到HWND

    HWND hwndFound //= FindWindow(_T("RC352_Win32"),NULL); = GetConsoleWindow();

  6. [Angular2 Form] patchValue, setValue and reset() for Form

    Learn how to update part of form model, full form model and reset whole form. We have form definetio ...

  7. TensorFlow on Windows: “Couldn't open CUDA library cudnn64_5.dll”

    TensorFlow on Windows: "Couldn't open CUDA library cudnn64_5.dll" 在 windows 下,使用 import te ...

  8. 【心情】"支NMLGB配树”

    大视野oj坏了 那就做杭电呗 看看大触都做杭电里的哪些题 看到杭电的分类了 Tarjan算法诶,我好像会嘛,就是你了 诶,怎么不是求强连通分量? 哦,原来是Tarjan算法的另外一个应用叫做支配树 我 ...

  9. [转] Python 爬虫的工具列表 附Github代码下载链接

    转自http://www.36dsj.com/archives/36417 这个列表包含与网页抓取和数据处理的Python库 网络 通用 urllib -网络库(stdlib). requests - ...

  10. 使Sublime Text支持除UTF8外多种编码 - ConvertToUTF8

    前几天,在使用了Sublime Text多天之后,感觉完全可以代替系统自带记事本,所以就通过其打开了一个记事本文件,但是打开之后尽然是乱码,在思考了一下之后想到记事本的编码是ANSI编码,那么这就应该 ...