叠加对话框 - 对齐视图

时间:2014-02-23 14:34:28

标签: android layout alignment overlay

我想为我的应用实施帮助覆盖。 它应该如下所示:How do I create a help overlay like you see in a few Android apps and ICS?

final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setContentView(R.layout.coach_mark);
dialog.setCanceledOnTouchOutside(true);
//for dismissing anywhere you touch
View masterView = dialog.findViewById(R.id.coach_mark_master_view);
masterView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        dialog.dismiss();
    }
});
dialog.show();

这非常有效。 coach_mark.xml的RelativeLayout显示在我通过setContentView()设置的现有布局上。 现在我想将coach_mark.xml的视图与现有布局的相应视图对齐,以便对分辨率和屏幕大小做出动态反应。这就是我的尝试:

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.BELOW,R.id.view_of_main_layout);
        view_of_the_overlay.setLayoutParams(lp);

但视图显示在屏幕中间。 我唯一能做到的就是将视图与屏幕底部对齐,但这不是我想要做的。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您可以使用PopupWindow:

PopupWindow popup=new PopupWindow(this);
popup.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
popup.setWindowLayoutMode(FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT);
popup.setContentView(R.layout.coach_mark);
View view=findById(R.id.view_of_main_layout);
int[] viewLocation=new int[2];
view.getLocationInWindow(viewLocation)
popup.showAtLocation(view, Gravity.TOP|Gravity.LEFT, viewLocation[0],viewLocation[1]);

您还可以使用最后两个变量(即X和Y offest)调整弹出位置。他们的价值观也可能是消极的。