Android 线性布局设置重力

Android 线性布局设置重力,android,android-linearlayout,gravity,layout-gravity,Android,Android Linearlayout,Gravity,Layout Gravity,我有框架布局,里面有四个按钮的线性布局。当我用xml编写代码时效果很好,但我想用java代码编写 这很有效 <FrameLayout ... <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="bottom"&g

我有框架布局,里面有四个按钮的线性布局。当我用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>
FrameLayout fl=findbyid。。。 f1.添加视图(待定)

试试看:

FrameLayout.LayoutParams pa = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT);
pa.gravity=80;
尝试:


你说“不行”是什么意思?如果你的意思是没有显示子视图,那是因为你没有添加它们。我的意思是,这不会下降…停留在屏幕的顶部。它没有任何包含的视图,那么你怎么知道呢?试着在里面添加一些视图。我没有粘贴所有代码,但我添加了四个按钮,我在屏幕上看到的很好,但不是在底部。你说的“不工作”是什么意思?如果你的意思是没有显示子视图,那是因为你没有添加它们。我的意思是,这不会下降…停留在屏幕的顶部。它没有任何包含的视图,那么你怎么知道呢?尝试在内部添加一些视图。我没有粘贴所有代码,但我添加了四个按钮,我可以在屏幕上看到它们,但在底部看不到。添加视图时,请执行:“f1.addView(tbr,pa);”我已经设置了LayoutParams…tbr.setLayoutParams(pa);但我也照你说的做了,但还是不担心。。我没有看到您将LinearLayout更改为FrameLayout…现在可以工作了…非常感谢您没有问题,最近也有相同的问题。我没有意识到,尽管您为LinearLayout设置了参数,但这取决于要选择的LayoutParams的父级。添加视图时,请执行:“f1.addView(tbr,pa);”我已经设置了LayoutParams…tbr.setLayoutParams(pa);但我也照你说的做了,但还是不担心。。我没有看到您将LinearLayout更改为FrameLayout…现在可以工作了…非常感谢您没有问题,最近也有相同的问题。我没有意识到,尽管为LinearLayout设置了参数,但这取决于要选择的LayoutParams的父级。
FrameLayout.LayoutParams pa = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT);
pa.gravity=80;