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. ...
随机推荐
- leetcode205
public class Solution { public bool IsIsomorphic(string s, string t) { if (s.Length != t.Length) { r ...
- div高度自适应的问题
对象height:100%并不能直接产生效果,是因为跟其父对象有关. #center{height:100%;} 上面的css样式是无效的,不会产生任何效果. 需要改写: html,body{ m ...
- 小菜鸟入门nginx
实现功能:端口进行转发 比如我实际运行的是·http:localhost:5000 但是我想通过localhost:80 进行访问. 过程 1 下载nginx 2 解压到某个目录(比如我放在C盘根目录 ...
- 在spring中使用quartz配置作业的二种方式
- CLR如何控制类型中的字段排序
[StructLayout(LayoutKind.Sequential) ]保持字段布局 [StructLayout(LayoutKind.Auto) ]CLR自动选择最优 [StructLayout ...
- s 销售视图数据
INSERT,需将公司ID匹对到以下EXCEL表 INSERT,需将公司ID匹对到以下EXCEL表 [Public] ConnectString=host="siebel://10.10.0 ...
- Golang之json序列化(struct,int,map,slice)
老规矩,直接上代码 package main import ( "encoding/json" "fmt" ) //把结构体都改小写 type User str ...
- TWO PHASES OF ANGULAR 2 APPLICATIONS
Angular 2 separates updating the application model and reflecting the state of the model in the view ...
- qmake -Visual Studio
创建 Visual Studio Projiect 文件 开发者使用 Visual Studio 写Qt应用可以使用Qt商业版提供的Visual Studio integration faciliti ...
- Linux驱动框架之misc类设备驱动框架
1.何为misc设备 (1)misc中文名就是杂项设备\杂散设备,因为现在的硬件设备多种多样,有好些设备不好对他们进行一个单独的分类,所以就将这些设备全部归属于 杂散设备,也就是misc设备,例如像a ...