帮助使用layoutinflator在运行时添加的看法? [英] help for using layoutinflator to add views at runtime?

查看:229
本文介绍了帮助使用layoutinflator在运行时添加的看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个布局:main.xml中和buttonpanel.xml。在buttonpanel.xml,主的LinearLayout我设置重力底部。现在我尝试使用下面的code添加buttonpanel布局。

I have two layouts: main.xml and buttonpanel.xml. In buttonpanel.xml, in main linearlayout I set gravity to bottom. Now i am trying to add the buttonpanel layout using the following code.

setContentView(R.layout.main);
LinearLayout layout=(LinearLayout)findViewById(R.id.mainlinearlayout);
LayoutInflater inflater= (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.buttonpanel,null);
layout.addView(view);

我的问题是,该小组被添加到顶部,虽然我已经在buttonpanel.xml设置重力底部。如果我添加buttonpanel.xml使用到main.xml中包括正常工作。

My problem is that the panel is added to the top though i have set gravity to bottom in buttonpanel.xml. If I add buttonpanel.xml to main.xml using include it works fine.

谁能帮我什么是错我的code?

Can anyone help me what is the wrong with my code?

推荐答案

我有问题,布局参数被夸大意见,你正在做的方式时下降。如果我用一个稍微不同的呼叫膨胀我的布局参数都得到尊重:

I have had problems with layout parameters being dropped when inflating views in the way that you are doing it. If I use a slightly different call to inflate my layout parameters are respected:

parent_view = inflater.inflate(R.layout.buttonpanel, parent);

或者对于我来说,当父母不支持添加的看法吧:

Or in my case, when the parent did not support adding views to it:

view = inflater.inflate(R.layout.buttonpanel, parent, false);

或许这将解决您的问题也是如此。

Perhaps this will solve your problem as well.

编辑:这取决于参数给出了不同的意见返回。 LayoutInflater

Different views are returned depending on what parameters are given. LayoutInflater

这篇关于帮助使用layoutinflator在运行时添加的看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆