11

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);

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.

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

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Hitendra
  • 3,218
  • 7
  • 45
  • 74
  • possible duplicate of [Making sense of LayoutInflater](http://stackoverflow.com/questions/5026926/making-sense-of-layoutinflater) – Bo Persson Jul 03 '12 at 12:49
  • Check this for a more detailed explanation: http://stackoverflow.com/questions/5026926/making-sense-of-layoutinflater – andig Feb 17 '11 at 09:16

2 Answers2

18

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.

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

leighman
  • 191
  • 6
Arlaharen
  • 3,095
  • 29
  • 26
0

you can use childview index for setting it. i think it will help you.try another method for addview which include childindex

chikka.anddev
  • 9,569
  • 7
  • 38
  • 46