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. ...
随机推荐
- ES6系列_9之对象
1.对象赋值 es5中的对象赋值方式如下: let name="小明"; let skill= 'es6开发'; var obj= {name:name,skill:skill}; ...
- mybatis相对于ibatis的优势
2010年,apache的Ibatis框架停止更新,并移交给了google团队,同时更名为MyBatis.从2010年后Ibatis在没更新过,彻底变成了一个孤儿框架.一个没人维护的框架注定被myba ...
- testng参数化(提供测试数据)
testng提供测试数据的两个注释:@DataProvide和@Parameter 一.通过testng.xml中设置参数 (实际上testng.xml只是一个名字,可以起任何一个名字,只要是.x ...
- MongoDB--CSharp Driver Quickstart .
原文链接 http://www.mongodb.org/display/DOCS/CSharp+Driver+Quickstart?showComments=true&showCommentA ...
- 【转】字符串匹配的KMP算法:移动位数 = 已匹配 - 部分匹配值(共有长度)
计算机的基本任务之一. 举例来说,有一个字符串"BBC ABCDAB ABCDABCDABDE",我想知道,里面是否包含另一个字符串"ABCDABD"? 许多算 ...
- What's App has the Qt?
收集了我看到的使用Qt开发的应用程序或者含有Qt库的应用程序 CNTV CNTV, 一个中央电视台的视频直播软件, 从下面卸载后的残余目录树,可以看到,存在部分库使用的就是Qt的.下面的目录树,已经删 ...
- [SoapUI] 通过Groovy脚本获取project所在的路径或者直接用${projectDir}
import com.eviware.soapui.support.GroovyUtils def groovyUtils = new GroovyUtils( context ) def proje ...
- 数据备份——PHP
在大多数情况下,开发实在win下进行,因此,然系统每天自动备份数据这也是有必要的饿. Windows平台数据备份 创建批处理文件 在批处理文件中填写如下代码: D:\wamp64\bin\php\ph ...
- 安卓开发解决android.os.NetworkOnMainThreadException异常方法(主线程不能直接调用webservice)
安卓开发解决android.os.NetworkOnMainThreadException异常方法 2013-01-07 14:01:04| 分类: 技术 | 标签:安卓 技术 java | ...
- python机器学习工具包scikit-learn
scikit-learn这个非常强大的python机器学习工具包 http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.ht ...