请教一个关于android动态布局的问题

fangzhen2005 2011-08-31 01:53:05
比如在XML里面定义了一个framelayout ,如果在代码而不是xml里面添加VIEW,怎样设置它的位置呢?用layout方法好像不管用?
...全文
366 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunmable 2011-09-01
  • 打赏
  • 举报
回复
换个思路,如果你在XML中会怎么作?

我是这样作的:
用一个相对布局添加view设置它与左上的间距。
添加方法一样。
然后再将这个相对布局添加到你的FrameLayout

在添加relativelayout时
是不是需要设置参数就要看你的须要了。
dinko321 2011-09-01
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 fangzhen2005 的回复:]

引用 13 楼 ch_984326013 的回复:

用addView方法添加

addView方法怎么定位,比如我定位到 左50px ,顶50px
[/Quote]

先把他add到布局,然后用他自己的layout摆过去
fangzhen2005 2011-08-31
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 ch_984326013 的回复:]

用addView方法添加
[/Quote]
addView方法怎么定位,比如我定位到 左50px ,顶50px
ch_984326013 2011-08-31
  • 打赏
  • 举报
回复
用addView方法添加
fangzhen2005 2011-08-31
  • 打赏
  • 举报
回复
FrameLayout.LayoutParams里面只有三个参数啊,怎么定位?
我在描述一下问题
1,比如我自定义一个CustomView 继承了 FrameLayout
2,我再定义一个CustomViews 继承了ViewGroup
3,我想在viewGroup 里面布置CustomView用代码布局,不用XML文件,如何操作,谢谢各位?
nulls 2011-08-31
  • 打赏
  • 举报
回复
学习了,还不知道RelativeLayout.LayoutParams的用法
fangzhen2005 2011-08-31
  • 打赏
  • 举报
回复
really,回去看看。先谢谢了。
sunmable 2011-08-31
  • 打赏
  • 举报
回复
frameLayout
没有用过说不好,也不好说。但是我相信是一样的。

FrameLayout.LayoutParams这个类中提供了你想要的所有布局参数,只须要设置一下,然后addview()方法中增加进去就可以了
fangzhen2005 2011-08-31
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sunmable 的回复:]

把你的Layout用layoutinf...拿出来,调用 它的一个方法叫作layout.addview(view,layoutparam);

第二个参数就是它的布局中的设置。

我给你上贴一些自己写的布局吧,这样可能比较清醒:
XML code

rp = new RelativeLayout.LayoutParams(layout_width / 3 - 1,
……
[/Quote]

谢谢,你的方法很给力~,那如果用frameLayout 怎么进行定位呢?
dinko321 2011-08-31
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 sunmable 的回复:]

也对,这个与findviewlayout不同的地方在于inflate可以读取文件而不是ID

findViewbyId是根据ID拿布局。

二楼的对不住了。
[/Quote]

findViewById需要在setContentView之后才能取到

inflate可以不setContentView就拿到





其实这个地方其实我很矛盾,也很纠结。

如果我需要用inflate去拿xml的布局,那我肯定要加载他,不然我写到XML去干什么,所以findViewById就更方便。但是如果我不想用XML的话,我直接全动态生成布局,再给setContentView

所以。。。。。
sunmable 2011-08-31
  • 打赏
  • 举报
回复
也对,这个与findviewlayout不同的地方在于inflate可以读取文件而不是ID

findViewbyId是根据ID拿布局。

二楼的对不住了。
dinko321 2011-08-31
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 sunmable 的回复:]

引用 2 楼 dinko321 的回复:

首先你要得到这个 frameLayout

一般是通过 findViewById

然后用 他的addView方法


不要用findViewById()这个方法拿到的是view而不是viewGroup

Java code
LayoutInflater inflater = (LayoutInflater) this.cont……
[/Quote]


public View inflate (int resource, ViewGroup root)
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为什么不用。。。。
sunmable 2011-08-31
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dinko321 的回复:]

首先你要得到这个 frameLayout

一般是通过 findViewById

然后用 他的addView方法
[/Quote]

不要用findViewById()这个方法拿到的是view而不是viewGroup

LayoutInflater inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.search_result, null);


我喜欢用这种方式拿
sunmable 2011-08-31
  • 打赏
  • 举报
回复
把你的Layout用layoutinf...拿出来,调用 它的一个方法叫作layout.addview(view,layoutparam);

第二个参数就是它的布局中的设置。

我给你上贴一些自己写的布局吧,这样可能比较清醒:

rp = new RelativeLayout.LayoutParams(layout_width / 3 - 1,
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
dinko321 2011-08-31
  • 打赏
  • 举报
回复
首先你要得到这个 frameLayout

一般是通过 findViewById

然后用 他的addView方法
fangzhen2005 2011-08-31
  • 打赏
  • 举报
回复
没人回答,再请教~

80,350

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧