比如在XML里面定义了一个framelayout ,如果在代码而不是xml里面添加VIEW,怎样设置它的位置呢?用layout方法好像不管用?
解决方案 »
- 如何快速播放MP3?
- [已解决]Android:ColorStateList的第一个参数为什么要用二维数组?(我觉得一维就够了)
- 如何修改android rom让其可以在不同的机型上用
- android的图片问题
- 做手机游戏需要哪些方面的知识?
- Android 可以连接其他类型数据库吗?
- 求助,关于麦克风问题
- 请教能否实现fragment内操控其fragmentActivity
- Android开发 ,如何读取AndroidMainfest.xml里面的节点信息
- android新手,求大神的指点啊
- 《android2.2自带通讯录》之联系人按拼音排序以及按汉字首字母或全拼搜索
- 重写onPause(),按下Back或Home键“程序意外停止”的问题
layout_height / 2 - 1);
rp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,
RelativeLayout.TRUE);
rp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,
RelativeLayout.TRUE);
this.addView(views.get(2), rp);这样可能比较明白一点,我用的是relativelayout
不要用findViewById()这个方法拿到的是view而不是viewGroupLayoutInflater inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.search_result, null);
我喜欢用这种方式拿
Since: API Level 1
Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.Parameters
resource 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. Returns
The 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.
这个东西的返回值也是一个View ,还是要通过向下强制转型有简单的findViewById为什么不用。
findViewById需要在setContentView之后才能取到inflate可以不setContentView就拿到其实这个地方其实我很矛盾,也很纠结。如果我需要用inflate去拿xml的布局,那我肯定要加载他,不然我写到XML去干什么,所以findViewById就更方便。但是如果我不想用XML的话,我直接全动态生成布局,再给setContentView所以。。
谢谢,你的方法很给力~,那如果用frameLayout 怎么进行定位呢?
没有用过说不好,也不好说。但是我相信是一样的。FrameLayout.LayoutParams这个类中提供了你想要的所有布局参数,只须要设置一下,然后addview()方法中增加进去就可以了
我在描述一下问题
1,比如我自定义一个CustomView 继承了 FrameLayout
2,我再定义一个CustomViews 继承了ViewGroup
3,我想在viewGroup 里面布置CustomView用代码布局,不用XML文件,如何操作,谢谢各位?
先把他add到布局,然后用他自己的layout摆过去
用一个相对布局添加view设置它与左上的间距。
添加方法一样。
然后再将这个相对布局添加到你的FrameLayout在添加relativelayout时
是不是需要设置参数就要看你的须要了。