一个控件应当使用它的父控件的 LayoutParams 类型。因此,一个 TableVow 应该使用 TableLayout.Params 。

所以,以一个 TableRow 为例:

        TableRow tableRow = new TableRow(context);
tableRow.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT,1.0f));

当我没有指明 LayoutParams 的类型时,TableRow 没有能够自动适应屏幕长度。因为 TableRow 不认识 TableLayout 的大小。所以此时即使设置了 weight 也不好使。

[android-developers] Re: Possible bug in TableLayout

Romain Guy Mon, 16 Feb 2009 19:57:16 -0800

Your code is *NOT* equivalent to the XML. You are using the wrong
LayoutParams everywhere. A widget must have the LayoutParams of its
*parent*. Therefore, the rows must have TableLayout.LayoutParams, the
TextViews must have TableRow.LayoutParams and the TableLayout must
have FrameLayout.LayoutParams.
Here is your code, corrected, and it works just fine.

package com.test;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView; public class Test extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(buildTableViewFromSource());
} private View buildTableViewFromSource() {
FrameLayout.LayoutParams pTable = new FrameLayout.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.FILL_PARENT); TableLayout table = new TableLayout(this);
table.setBackgroundColor(Color.RED);
table.setLayoutParams(pTable); TableRow rowTop = new TableRow(this); TableLayout.LayoutParams pRowTop = new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
pRowTop.weight = 1; rowTop.setBackgroundColor(Color.BLUE); TextView txt = new TextView(this);
txt.setText("Top Content"); rowTop.addView(txt, new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT)); TableRow rowBottom = new TableRow(this);
rowBottom.setBackgroundColor(Color.GREEN); TextView txtBottom = new TextView(this);
txtBottom.setText("Bottom Content"); TableLayout.LayoutParams pRowBottom = new
TableLayout.LayoutParams(
TableLayout.LayoutParams.WRAP_CONTENT,
TableLayout.LayoutParams.WRAP_CONTENT); rowBottom.addView(txtBottom, new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT)); table.addView(rowTop, pRowTop);
table.addView(rowBottom, pRowBottom); return table;
}
}

Android 中 LayoutParams 的用法的更多相关文章

  1. Android中LayoutParams的用法

    简单说说 自己对 android LayoutParams的理解吧,xh写不出高级文章是低级写手.public static classViewGroup.LayoutParamsextends Ob ...

  2. android 中uri.parse()用法

    android 中uri.parse()用法 1,调web浏览器 Uri myBlogUri = Uri.parse("http://xxxxx.com"); returnIt = ...

  3. Android中Selector的用法(改变ListView和Button的默认背景)

    Android中的Selector的用法 http://blog.csdn.net/shakespeare001/article/details/7788400#comments Android中的S ...

  4. Android中Intent的用法总结

    Intent只在Android中特有,我把它比作一种运载工具,就像飞机一样,会把一些人带到某个地方,而且如果需要的话,还可以找到机上有哪些人员(数据),这就需要另外一些设备来支持(如:Bundle), ...

  5. android中sharedPreferences的用法

    SharedPreferences介绍:   做软件开发应该都知道,很多软件会有配置文件,里面存放这程序运行当中的各个属性值,由于其配置信息并不多,如果采用数据库来存放并不划算,因为数据库连接跟操作等 ...

  6. 【转】Android中Application类用法

    转自:http://www.cnblogs.com/renqingping/archive/2012/10/24/Application.html Application类 Application和A ...

  7. Android中Application类用法

    Application类 Application和Activity,Service一样是Android框架的一个系统组件,当Android程序启动时系统会创建一个Application对象,用来存储系 ...

  8. 【Android】Android 中string-array的用法

    在Android中,用string-array是一种简单的提取XML资源文件数据的方法. 例子如下: 把相应的数据放到values文件夹的arrays.xml文件里 <?xml version= ...

  9. (转)Android中Parcelable接口用法

    1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...

随机推荐

  1. Nginx代理配置文件

    #user nginx; worker_processes 5; #error_log /var/log/nginx/error.log warn; #pid /var/run/nginx.pid; ...

  2. Windows下cpu使用的监控

    这里有一篇来自微软的文章,主要讲WPA如何使用,但是前面关于进程调度的介绍很给力,非常好的学习操作系统的材料: https://msdn.microsoft.com/en-us/library/jj6 ...

  3. SpringBoot Session 管理及集群管理

    1.配置session的超时时间 : 在application.prooperties中 server.session.timeout = 600       //以秒为单位,默认最少一分钟 2.配置 ...

  4. IE浏览器中不支持cookie问题

    /** * Cookie plugin * * Copyright (c) 2006 Klaus Hartl (stilbuero.de) * Dual licensed under the MIT ...

  5. post get 区别

    GET 请求能被缓存,POST不能: POST 相对安全,GET的请求都包含在URL里 POST 可以通过request body 来传输较多的数据 URL有长度限制,会影响到GET请求,这个长度是由 ...

  6. eclipse中项目出现红色的!

    eclipse中项目出现红色的!的原因有二个:1.jdk不匹配    2.缺少jar包

  7. win7局域网内共享文件夹及安全设置

    右键想要共享的文件夹,选择属性. 在文件夹属性对话框中选择共享标签,如图: 点击共享按钮,弹出文件共享对话框. 添加 Everyone ,并根据实际需要修改权限.权限可以是读取 或 读取/写入. 到此 ...

  8. Spring IOC容器启动流程源码解析(四)——初始化单实例bean阶段

    目录 1. 引言 2. 初始化bean的入口 3 尝试从当前容器及其父容器的缓存中获取bean 3.1 获取真正的beanName 3.2 尝试从当前容器的缓存中获取bean 3.3 从父容器中查找b ...

  9. Visual Studio C++ include与library

    首先介绍几种目录: 1. 系统路径 系统路径在vc中是"Properties->Configuration Properties -> VC++ Directories" ...

  10. curl模拟多线程抓取网页(优化)

    通过上篇文章清楚了通过curl_multi_*函数可以一次请求多个url,但是也留下了问题,就是结果要等所有数据请求结束一起返回,才能逐个处理数据.优化代码,使先成功请求的url先返回处理结果,而不是 ...