博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
安卓动态增加按钮
阅读量:4598 次
发布时间:2019-06-09

本文共 1667 字,大约阅读时间需要 5 分钟。

首先了解一下LayouInflater类:将一个layout xml文件实例化为相应的View 对象。他永远不会直接使用,而是使用 getLayoutInflater() 或者 getSystemService(String) 来获得标准的LayoutInflater实例,这个实例已经将context连接起来,为正在运行的设备配置正确。比如:[java] view plaincopyLayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  public View inflate (int resource, ViewGroup root)Since: API Level 1Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.Parametersresource     ID for an XML layout resource to load (e.g., R.layout.main_page) root     Optional view to be the parent of the generated hierarchy.ReturnsThe root View of the inflated hierarchy. If root was supplied, this is the root View; otherwise it is the root of the inflated XML file.假如有一个这样的xml文件(guess_button.xml) 放入到一个buttonTableLayout:[html] view plaincopy
(guess_button.xml)[html] view plaincopy
(main.xml中定义的需要被扩充的buttonTableLayout,假设其在程序中的引用也叫buttonTableLayout, 即buttonTableLayout =(TableLayout) findViewById(R.id.buttonLayout);)现在我们将其第一行进行扩充:[java] view plaincopyLayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); Button newGuessButton =(Button)inflater.inflater(R.layout.guess_button,null); //获得按钮 newGuessButton.setText("abc"); //设置按钮文本 newGuessButton.setOnClickListener(guessButtonListener); //设置Listener (TableRow) buttonTableLayout.getChildAt(1); //将newGuessButton加到table第一行 OK 整个流程就完成了

转载于:https://www.cnblogs.com/zlja/archive/2012/04/13/2446576.html

你可能感兴趣的文章
_bzoj2005 [Noi2010]能量采集
查看>>
pat 团体天梯赛 L3-010. 是否完全二叉搜索树
查看>>
烟草MES系统介绍-序
查看>>
优先队列小结
查看>>
线程安全与可重入函数之间的区别与联系
查看>>
bat批处理中如何获取前一天日期
查看>>
{Nodejs} request URL 中文乱码
查看>>
异常及日志使用与项目打包
查看>>
努力,时间,坚持,自律
查看>>
真三 bug PT的凤凰
查看>>
???动态SQL
查看>>
js错误处理与调试理论和办法
查看>>
Binding.StringFormat不起作用的原理和解决
查看>>
css hack兼容写法
查看>>
CSS两列布局 一边固定 一边自适应
查看>>
Hadoop2.6.0 动态增加节点
查看>>
图论的一些概念、定理
查看>>
WebView用法
查看>>
Lecture 3: Planning by Dynamic Programming
查看>>
用flash代替图片IMG,设置动态效果链接
查看>>