FrameLayout layout_weight以编程方式

时间:2011-12-20 01:49:27

标签: android android-layout

我正在尝试以编程方式创建FrameLayout,如下所示(此视图将浮动在LinearLayout的底部中心):

    FrameLayout bottomFrameLayout = new FrameLayout(context);
    bottomFrameLayout.setLayoutParams(new FrameLayout.LayoutParams(0,LayoutParams.FILL_PARENT,Gravity.CENTER | Gravity.BOTTOM));

由于我设置layout_width=0dp,我还想设置layout_weight来控制宽度。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:17)

您需要使用LinearLayout.LayoutParams (int width, int height, float weight)构造函数,因为FrameLayoutLinearLayout的孩子,因此您要为LinearLayout中的孩子设置参数。

编辑:

 FrameLayout bottomFrameLayout = new FrameLayout(context);
 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0,LayoutParams.FILL_PARENT,mWeight);
 lp.gravity = Gravity.CENTER | Gravity.BOTTOM;
 bottomFrameLayout.setLayoutParams(lp);