弹出式视图(z-index?) - android

发布于 2024-09-15 05:40:35 字数 387 浏览 6 评论 0原文

我想向我的场景添加一个新视图,该视图将包含在我的应用程序过程中以编程方式更改的内容。当它发生变化时,它需要在屏幕上弹出 3 秒(或单击一下)然后消失。

该视图将根据其内容 WRAP_CONTENT 改变大小,但理想情况下我希望它在屏幕上水平和垂直居中。

我被困在三个部分: 1)我应该使用什么类型的视图......我在考虑相对,但我所有的使用它都没有为我想做的事情产生好的结果 2)关于#1(尝试相对视图),我无法让它正确居中(尝试使用具有不同值的param.leftMargin和param.topMargin,但无法让它在具有不同分辨率的不同设备上工作 3)同样关于#1,我无法让这个浮动在屏幕上的其他所有内容上(需要像 z-index 之类的东西)。

任何想法、代码示例都会很棒。

TIA

I want to add a new view to my scene that will contain content that will change programatically throughout the course of my application. When it does change, it needs to pop up on the screen for 3 seconds (or a click) then disappear.

This view will change in size according to its content WRAP_CONTENT, but ideally I'd like it centered horizontally and vertically on the screen.

I'm stuck on three parts:
1) what type of view should I use for this...I was thinking Relative, but all of my playing with it has yielded no good results for what I'm trying to do
2) with respect to #1 (trying relative view), I could not get it centered properly (tried using param.leftMargin and param.topMargin with varying values but could not get it to work on different devices with different resolutions
3) also with respect to #1, I couldn't make this float over everything else on my screen (need something like a z-index or the like).

any ideas, code examples would be wonderful.

TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光清浅 2024-09-22 05:40:35

使用自定义对话框,即带有 android:theme="@android:style/Theme.Dialog" 的 LinearLayout,对于该类,它类似于

public class YourCustomDialog extends Dialog implements DialogInterface

您可以在其中实现自定义逻辑的地方展示。这样的对话框是浮动的,并且是模态的,位于所有其他视图之上,您还可以选择将背景设置为模糊等。

这是我的自定义对话框的典型构造函数 - 布局将在 xml 布局文件中定义,该文件在我的案例是 my_custom_dialog.xml:

public MyCustomDialog(Context context) {
    super(context, android.R.style.Theme);

    Window window = getWindow();
    window.requestFeature(Window.FEATURE_NO_TITLE);
    window.setGravity(Gravity.BOTTOM);
    window.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.empty));

    setContentView(R.layout.my_custom_dialog);

    // actually not necessary as it's already the default value:
    window.setLayout(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    ...
}

Use a custom dialog, i.e. a LinearLayout with android:theme="@android:style/Theme.Dialog" and for the class, it would be something like

public class YourCustomDialog extends Dialog implements DialogInterface

where you can implement you custom logic of what to display. Such dialog is floating and modal on top of all other views then and you can also optionally set the background to blurry, etc.

This is a typical constructor of my custom dialog - the layout would be defined in an xml layout file, which in my case is my_custom_dialog.xml:

public MyCustomDialog(Context context) {
    super(context, android.R.style.Theme);

    Window window = getWindow();
    window.requestFeature(Window.FEATURE_NO_TITLE);
    window.setGravity(Gravity.BOTTOM);
    window.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.empty));

    setContentView(R.layout.my_custom_dialog);

    // actually not necessary as it's already the default value:
    window.setLayout(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文