如何使用layoutinflator在运行时添加的看法? [英] How to use layoutinflator to add views at runtime?
问题描述
我有两个布局: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屋!