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. ...
随机推荐
- Matlab信号处理工具箱函数
波形产生和绘图chirp 产生扫描频率余弦diric 产生Dirichlet函数或周期Sinc函数gauspuls 产生高斯调制正弦脉冲pulstran 产生脉冲串rectpuls 产生非周期矩形信号 ...
- Sql语句在线转java bean https://www.bejson.com/othertools/sql2pojo/
https://www.bejson.com/othertools/sql2pojo/
- oreilly 用户故事地图
这本书是完全买亏了,一点作用也没有. 整篇有用的字很少,还花了我¥16,总结如下: 用户故事模板: 作为用户角色(who),我想要某项功能(what),这样我可以 XXX(原因,why)
- LDAP属性对照表
AD属性对照表 姓 Sn 名 Givename 英文缩写 Initials 显示名称 displayName 描述 Description 办公室 physicalDeliveryOfficeName ...
- 跟我学算法-吴恩达老师(超参数调试, batch归一化, softmax使用,tensorflow框架举例)
1. 在我们学习中,调试超参数是非常重要的. 超参数的调试可以是a学习率,(β1和β2,ε)在Adam梯度下降中使用, layers层数, hidden units 隐藏层的数目, learning_ ...
- ScheduledThreadPoolExecutor 线程池调度 使用
package other; import java.util.concurrent.Callable; import java.util.concurrent.Executors; import j ...
- c++经典排序算法全集(转)
C++排序算法全集 排序算法是一种基本并且常用的算法.由于实际工作中处理的数量巨大,所以排序算法对算法本身的速度要求很高. 一.简单排序算法 由于程序比较简单,所以没有加什么注释.所有的程序都给出了完 ...
- [ML]熵、KL散度、信息增益、互信息-学习笔记
[ML]熵.KL散度.信息增益.互信息-学习笔记 https://segmentfault.com/a/1190000000641079
- Python pip配置国内源
众所周知,Python使用pip方法安装第三方包时,需要从 https://pypi.org/ 资源库中下载,但是会面临下载速度慢,甚至无法下载的尴尬,这时,你就需要知道配置一个国内源有多么重要了,通 ...
- 90. Subsets II (Back-Track, DP)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...