1

I have frame layout and inside of it linear layout with four buttons. When i write in xml works great but i want to write in java code.

THIS WORKS

<FrameLayout ...
<LinearLayout   
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:layout_gravity="bottom">
    <ImageButton
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <ImageButton
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <ImageButton
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>
</FrameLayout>

BUT THIS DOESN't WORK

tbr = new LinearLayout(getBaseContext());
LinearLayout.LayoutParams pa = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
pa.gravity = Gravity.BOTTOM; ///??
tbr.setLayoutParams(pa);

FrameLayout fl = findbyid... f1.addView(tbr);

rikkima
  • 95
  • 1
  • 11
  • 1
    What do you mean by "doesn't work"? If you mean that the child views are not shown, it is because you haven't added them. – Jong Jul 04 '13 at 19:51
  • I mean that doesn't get down ...Stay at the top of screen – rikkima Jul 04 '13 at 19:59
  • It doesn't have any contained views, so how can you tell? Try to add some views inside. – Jong Jul 04 '13 at 20:03
  • i didn't paste all code but i add four buttons which i see on the screen well but not at the bottom – rikkima Jul 04 '13 at 20:05

1 Answers1

1

Try:

FrameLayout.LayoutParams pa = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT);
pa.gravity=80;
bouscher
  • 1,300
  • 1
  • 9
  • 18
  • When you add the view, do: "f1.addView(tbr,pa);" – bouscher Jul 04 '13 at 19:57
  • i already setLayoutParams ...tbr.setLayoutParams(pa); but i also try as you said and still doesn't work – rikkima Jul 04 '13 at 20:02
  • Sorry .. i didn't see that you changed LinearLayout to FrameLayout ...Now works ...thank you a lot – rikkima Jul 04 '13 at 20:24
  • No problem, had the same issue recently. I didn't realize that although you set the params for a LinearLayout it depends on the parent which LayoutParams to choose. – bouscher Jul 04 '13 at 20:31