2013-07-04 42 views
1

我有框架布局和其内部的四个按钮线性布局。当我在xml中编写伟大工程,但我想用java代码编写。LinearLayout设置重力

这适用

<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这不起作用

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

+1

你说的 “不工作” 是什么意思?如果你的意思是没有显示子视图,那是因为你没有添加它们。 – Jong

+0

我的意思是说不会停下来......留在屏幕的顶部 – rikkima

+0

它没有任何包含的视图,所以你怎么知道?尝试在里面添加一些视图。 – Jong

回答

1

尝试:

FrameLayout.LayoutParams pa = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT); 
pa.gravity=80; 
+0

不起作用..无论如何,感谢 – rikkima

+0

当您添加视图时,请执行:“f1.addView(tbr, PA);” – bouscher

+0

我已经setLayoutParams ... tbr.setLayoutParams(pa); 但我也尝试,因为你说,仍然无法工作 – rikkima