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都是可以正确显示的,不相信的朋友,自己也可以试试看。至于这到底是什么原因呢,我目前也不清楚,希望有知道的朋友留言指教一下,谢谢

最新文章

  1. Go语言实战
  2. 5.SVM核函数
  3. Markdown会干掉Html吗?
  4. c/c++面试题(2)
  5. iOS打包为ipa的两种方式和生成P12证书的方式
  6. javascript get获取参数
  7. USACO Party Lamps 【Binary code solvution】【规律】
  8. SOA两个接口通常用于实现更:SOAP vs REST
  9. jquery验证表单是否满足正则表达式是否通过验证例子
  10. unity传送门类似效果实现
  11. Red and Black
  12. PHP call_user_func
  13. 使用c语言实现linux数据库的操作
  14. 注解Responsebody RequestBody RequestMapping
  15. vue2.0实现前端星星评分功能组件
  16. Java 去掉字符串中的换行符回车符等
  17. 干货 | Java8 新特性教程
  18. 用Margin还是用Padding?(转载)
  19. python微信自动回复
  20. Kaldi的data目录解析

热门文章

  1. Spring Boot 静态资源处理(转)
  2. 结合Wireshark捕获分组深入理解TCP/IP协议栈之DNS协议
  3. 12、USB设备驱动程序
  4. [React] Setup &#39;beforeunload&#39; listener
  5. Facebook Hacker Cup 2015 Round 1--Winning at Sports(动态规划)
  6. C#验证手机号
  7. C#中使用split分割字符串的几种方法小结
  8. Linux中LVM2原理及制作LVM2
  9. 如何查看Outlook邮件的源码(包括ip)
  10. HelloWorld RabbitMQ