如何将ViewGroup与屏幕底部对齐

时间:2014-09-19 19:44:18

标签: android alignment relativelayout viewgroup layoutparams

所以,我有一个作为对话框(Theme.Dialog)启动的活动。我用它来播放视频,调暗背景,看起来很酷......但它始终以屏幕为中心,并且不知道如何以编程方式将其移到底部。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/lr_layout"
            android:background="#FF000000"
            android:padding="0dp" >


<RelativeLayout
    android:id="@+id/lr_progressContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF000000">

    <ProgressBar android:id="@+id/lr_progress"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:background="@android:color/black"
      style="@android:style/Widget.ProgressBar.Large" />

    <View android:id="@+id/disableShape"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="0dp"
    android:visibility="gone"
    android:background="#CC000000" 
    />
</RelativeLayout>

</RelativeLayout>

然后我以编程方式添加视频播放器(从第三方查看)。

我试过这个:

layout = (ViewGroup) findViewById(R.id.lr_layout);
layout.setY(0);

和此:

RelativeLayout.LayoutParams adViewLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
adViewLayoutParams.addRule(RelativeLayout.ALIGN_BOTTOM);
layout.addView(adView, adViewLayoutParams);

但视频仍在中心位置。

有什么想法吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

尝试将其放入onCreate方法中。

WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.gravity = Gravity.BOTTOM; getWindow().set attributes(lp);

答案 1 :(得分:0)

如果要将relativeLayout子对齐到底部,可以设置alignParentBottom = true 如果你想将对话框对齐到屏幕底部,你可以改用popupWindow!