1

我有框架布局,里面有四个按钮的线性布局。当我用 xml 编写时效果很好,但我想用 java 代码编写。

这行得通

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

但这不起作用

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

4

1 回答 1

1

尝试:

FrameLayout.LayoutParams pa = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT);
pa.gravity=80;
于 2013-07-04T19:51:57.130 回答