在活动之上创建一个透明的对话框 [英] Create a transparent dialog on top of activity

查看:205
本文介绍了在活动之上创建一个透明的对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景



我试图在当前活动的顶部放置一层,这将解释当前屏幕上发生的情况,类似于发生在联系人+应用上。



我知道有一些解决方案(如 showCase库和superToolTips库),我也知道我可以创建一个视图并将其设置在顶部,将其添加到活动的窗口中,但是我需要放置一个整个对话框层



问题



无论我尝试什么,每个解决方案都不符合我的需要



简而言之,我需要的是:




  • 全屏对话框。


  • 没有更改(不可视而不是逻辑)到操作栏,notif ication ication bar bar bar of and and meaning meaning meaning meaning meaning transparent transparent transparent transparent transparent transparent transparent transparent transparent transparent transparent transparent transparent transparent transparent transparent transparent transparent。。。。。。。。。。。。。。。。。。。transparent transparent transparent transparent




我尝试过的 h2>

令人遗憾的是,我一直只有一部分我需要的东西。



这是我的代码: p>

style.xml:

 < style name =full_screen_dialog> ; 
< item name =android:windowFrame> @ null< / item>
< item name =android:windowIsFloating> true< / item>
< item name =android:windowContentOverlay> @ null< / item>
< item name =android:windowAnimationStyle> @android:style / Animation.Dialog< / item>
< item name =android:windowSoftInputMode> stateUnspecified | adjustPan< / item>
< / style>

MainActivity.java:

  ... 
final Dialog dialog = new Dialog(this,R.style.full_screen_dialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.floating_tutorial);
dialog.getWindow()。setLayout(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
dialog.getWindow()。setFormat(PixelFormat.TRANSLUCENT);
dialog.show();

此代码将把布局放在活动的顶部,但遗憾的是它没有任何透明度即使我已经设置好了。我使用的布局非常简单,这就是为什么我不发表。



问题



我失踪了什么?应该做什么来修复代码?



如何让对话框透明,全屏,并且不会更改操作栏和通知栏。






工作解决方案



编辑:找到一个很好的解决方案后,这里是工作代码:

  @Override 
protected void onCreate(final Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.floating_tutorial);
final Window window = dialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}


解决方案

只需更改背景颜色您的对话框

  dialog.getWindow()。setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 

编辑:



效果:

  dialog.getWindow()。clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); 


Background

I'm trying to put a layer on top of the current activity which would have explanation of what is going on on the current screen, similar to what occurs on contact+ app .

I know there are some solutions for this (like the showCase library and the superToolTips library ) , and I also know that I can create a view and set it on top by adding it to the window of the activity , but I need put a whole dialog layer on top.

Problem

No matter what I try, each solution doesn't work the way I need it to work.

in short , what I need is:

  • full screen dialog.

  • no change (not visual and not logical) to the action bar, notification bar, and content of the activity behind, meaning that everything behind the dialog stays the same it was shown a moment before the dialog was shown.

  • be transparent except for the views I use for the dialog, which should be shown normally.

what I've tried

Sadly, I've always got only a part of the things I needed.

here's my code:

styles.xml:

<style name="full_screen_dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    </style>

MainActivity.java:

...
final Dialog dialog = new Dialog(this, R.style.full_screen_dialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.floating_tutorial);
dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
dialog.getWindow().setFormat(PixelFormat.TRANSLUCENT);
dialog.show();

This code will put the layout on top of the activity, but sadly it doesn't have any transparency , even though I've set it . The layout I've used is very simple which is why I don't post it.

Question

what am I missing ? what should be done to fix the code?

how can I make the dialog both transparent, full screen AND that it won't change the action bar and notifications bar.


Working solution

EDIT: after finding a good solution, here's the working code:

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.floating_tutorial);
    final Window window = dialog.getWindow();
    window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
    window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    dialog.show();
}

解决方案

Just change the background color of your Dialog:

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

Edit:

This prevents the dim effect:

dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

这篇关于在活动之上创建一个透明的对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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