Android - 在FrameLayout中将RelativeLayout对齐居中

时间:2014-07-24 02:23:44

标签: java android

adView是AdMob的视图,我将其添加到另一个框架视图中,如下所示:

adView = new AdView(this);

RelativeLayout relativeLayout = new RelativeLayout(this);
mFrameLayout.addView(relativeLayout);


RelativeLayout.LayoutParams adViewParams = new RelativeLayout.LayoutParams(
   AdView.LayoutParams.WRAP_CONTENT,
   AdView.LayoutParams.WRAP_CONTENT);
   //important I use this to make it appear on the bottom
adViewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);


relativeLayout.addView(adView, adViewParams);

它出现在框架布局的底部,因此位于屏幕的底部(框架布局是我的屏幕)。但是不是在中心,它是左对齐的。如何对RelativeLayout中的FrameLayout进行对齐。

EIDT:如果你需要它,这是FrameLayout:

    // FrameLayout
    ViewGroup.LayoutParams framelayout_params =
        new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                   ViewGroup.LayoutParams.MATCH_PARENT);
    mFrameLayout = new FrameLayout(this);
    mFrameLayout.setLayoutParams(framelayout_params);

1 个答案:

答案 0 :(得分:1)

我做到了这一点它有效:

adViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);

请确认是否正确。