Android 中 LayoutParams 的用法
一个控件应当使用它的父控件的 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
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 的用法的更多相关文章
- Android中LayoutParams的用法
简单说说 自己对 android LayoutParams的理解吧,xh写不出高级文章是低级写手.public static classViewGroup.LayoutParamsextends Ob ...
- android 中uri.parse()用法
android 中uri.parse()用法 1,调web浏览器 Uri myBlogUri = Uri.parse("http://xxxxx.com"); returnIt = ...
- Android中Selector的用法(改变ListView和Button的默认背景)
Android中的Selector的用法 http://blog.csdn.net/shakespeare001/article/details/7788400#comments Android中的S ...
- Android中Intent的用法总结
Intent只在Android中特有,我把它比作一种运载工具,就像飞机一样,会把一些人带到某个地方,而且如果需要的话,还可以找到机上有哪些人员(数据),这就需要另外一些设备来支持(如:Bundle), ...
- android中sharedPreferences的用法
SharedPreferences介绍: 做软件开发应该都知道,很多软件会有配置文件,里面存放这程序运行当中的各个属性值,由于其配置信息并不多,如果采用数据库来存放并不划算,因为数据库连接跟操作等 ...
- 【转】Android中Application类用法
转自:http://www.cnblogs.com/renqingping/archive/2012/10/24/Application.html Application类 Application和A ...
- Android中Application类用法
Application类 Application和Activity,Service一样是Android框架的一个系统组件,当Android程序启动时系统会创建一个Application对象,用来存储系 ...
- 【Android】Android 中string-array的用法
在Android中,用string-array是一种简单的提取XML资源文件数据的方法. 例子如下: 把相应的数据放到values文件夹的arrays.xml文件里 <?xml version= ...
- (转)Android中Parcelable接口用法
1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...
随机推荐
- django-admin:command not found的解决办法
django-admin:command not found的解决办法 找到django-admin的路径 绝对路径 然后用命令行运行 python3 /usr/local/python3/lib/ ...
- ADO Connection failure
使用ado连接,频繁报错.为何?是网络的问题吗?太灵敏了.Connection failure. 跟大文本又关系??
- css常用属性初总结:font
平时在做项目时,UX常说的一句话就是“这里的字体不对吧,字体大小也不太对,你们前端有没有按规范来”,今天,我们就一起来看看这折磨人的font属性. 字体属性font-family 允许值 系列名 初始 ...
- Axure 原型图 (转)
Axure RP是很有名的一个界面原型设计工具,可以灵活快捷的对C/S.B/S程序设计原型. 近期我要开发一个Android客户端,也打算使用Axure RP设计原型. 下载地址:http://pan ...
- leetcode 121 股票买卖问题系列
描述: 给一些列数字,表示每条股票的价格,如果可以买卖一次(不能同一天买和卖),求最大利益(即差最大). 其他三道问题是,如果能买卖无限次,买卖两次,买卖k次. 题一: 实质是求后面一个数减前一个数的 ...
- leetcode 204 count prim 数素数
描述: 给个整数n,计算小于n的素数个数. 思路: 埃拉托斯特尼筛法,其实就是普通筛子,当检测到2是素数,去除所有2的倍数:当检测到3是素数,去除其倍数. 不过这要求空间复杂度为n,时间复杂度为n. ...
- Spring Boot 、mybatis 、swagger 和 c3p0 整合
文件路径如下 添加依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...
- Entity Framework 6.0 对枚举的支持/实体添加后会有主键反回
实验 直接上代码,看结果 实体类 [Flags] public enum FlagsEnum { Day = , Night = } public class EntityWithEnum { pub ...
- php识别二维码
php-zbarcode 是 PHP 读取条形码的扩展模块,目前仅支持 php5.x
- MySQL 系列(三)事务
MySQL 系列(三)事务 一组要么同时执行成功,要么同时执行失败的 SQL 语句.是数据库操作的一个执行单元! 事务开始于: 连接到数据库上,并执行条 DML 语句(INSERT. UPDATE 或 ...