叠加对话框-对齐视图 [英] Overlay dialog - align view

查看:81
本文介绍了叠加对话框-对齐视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的应用实现帮助叠加. 它应该看起来像这样:

I want to implement a help overlay for my app. It should look like this: 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的视图,以便对分辨率和屏幕尺寸做出动态反应.这是我尝试过的:

This works very well. The RelativeLayout of coach_mark.xml is displayed over my existing layout which was set via setContentView(). Now I want to align the views of coach_mark.xml near the according views of the existing layout, in order to react dynammically to the resolution and screen size. This is what I have tried:

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

但是视图仅显示在屏幕中间. 我唯一可以存档的是将视图与屏幕底部对齐,但这不是我想要的.

But the view is displayed just in the middle of the screen. The only thing I could archieve is to align the view to bottom of screen, but that is not what I want to do.

感谢您的帮助.

推荐答案

您可以使用PopupWindow:

You can use 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来调整弹出窗口的位置.它们的值也可以是负数.

you can also adjust to popup location with the last two variables which are the X and Y offest. Their values can also be negative.

这篇关于叠加对话框-对齐视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆