我如何像您在一些 Android 应用程序和 ICS 中看到的那样创建帮助覆盖?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10216937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create a help overlay like you see in a few Android apps and ICS?
提问by ssuperz28
I am wanting to create help overlays like the ones you see when ICS loads for the first time or in apps like ES File Explorer or Apex Launcher (there are more, but I can't think of them right now). Is this just a relative layout with one view sitting on top of the other? I haven't been able to find any sample code for doing such a thing. Anyone know how this is done or have any ideas?
我想创建帮助覆盖,就像您在首次加载 ICS 或在 ES File Explorer 或 Apex Launcher 等应用程序中看到的那样(还有更多,但我现在想不起来)。这只是一个视图位于另一个视图之上的相对布局吗?我一直无法找到任何示例代码来做这样的事情。任何人都知道这是如何完成的或有任何想法吗?
采纳答案by CommonsWare
Let's assume you ordinarily would call setContentView(R.layout.main)
, but on first run, you want to have this overlay.
假设您通常会调用setContentView(R.layout.main)
,但在第一次运行时,您希望拥有此叠加层。
Step #1: Create a FrameLayout
in Java code and pass that to setContentView()
.
第 1 步:FrameLayout
在 Java 代码中创建一个并将其传递给setContentView()
.
Step #2: Use LayoutInflater
to inflate R.layout.main
into the FrameLayout
.
第 2 步:LayoutInflater
用于充气R.layout.main
到FrameLayout
.
Step #3: Use LayoutInflater
to inflate the overlay into the FrameLayout
.
第 3 步:用于LayoutInflater
将覆盖层充气到FrameLayout
.
Step #4: When the user taps the button (or whatever) to dismiss the overlay, call removeView()
to remove the overlay from the FrameLayout
.
第 4 步:当用户点击按钮(或其他任何东西)关闭覆盖层时,调用removeView()
从FrameLayout
.
Since the overlay is a later child of the FrameLayout
, it will float over top of the contents of R.layout.main
.
由于覆盖层是 的后续子项FrameLayout
,因此它将浮动在 的内容之上R.layout.main
。
回答by Oded Breiner
"Coach mark" is "Help overlay" in UX talk :-)
“教练标记”是用户体验谈话中的“帮助覆盖”:-)
coach_mark.xmlis your coach mark layout
Coach_mark.xml是您的教练标记布局
coach_mark_master_viewis the id of the top most view (root) in coach_mark.xml
Coach_mark_master_view是coach_mark.xml中最顶层视图(根)的id
public void onCoachMark(){
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();
}
Adding sample of coach_mark.xml (to this excellent solution given by Oded Breiner), so its easy for ppl to copy & paste to see working example quickly.
添加 Coach_mark.xml 示例(添加到 Oded Breiner 提供的这个优秀解决方案中),因此 ppl 可以轻松复制和粘贴以快速查看工作示例。
Sample of coach_mark.xml here, change the -> drawable/coach_marks to your image:
此处的 Coach_mark.xml 示例,将 -> drawable/coach_marks 更改为您的图像:
coach_mark.xml
Coach_mark.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/coach_mark_master_view">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/coach_marks_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal"
android:src="@drawable/coach_marks" />
</RelativeLayout>
</LinearLayout>
And optionally use this theme to remove padding:
并可选择使用此主题来删除填充:
<style name="WalkthroughTheme" parent="Theme.AppCompat">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
回答by FUBUs
You can do that pretty quickly. You add, for exemple a LinearLayout where you put a picture with alpha which correspond to your help information and what do you want to draw like an overlay. In you xml of your activity you put this layout in a RelativeLayout after the layout of your activity with the Gone visibility. When you want to draw the help information, you just neeed to set this visibility to visible.
你可以很快地做到这一点。例如,您添加了一个 LinearLayout,您可以在其中放置一张带有 alpha 的图片,该图片对应于您的帮助信息以及您想像覆盖图一样绘制什么。在您的活动的 xml 中,您将此布局放置在具有 Gone 可见性的活动布局之后的 RelativeLayout 中。当你想绘制帮助信息时,你只需要将此可见性设置为可见即可。
I hope, I'm clear, if you have any question,I'm be please to answer them.
我希望,我很清楚,如果你有任何问题,我很乐意回答他们。
回答by Whome
See my another answer how programmatically show an overlay layout on top of the current activity. Activity's layout.xml does not need to know anything about the overlay skin. You can put overlay semi-transparent, cover only part of the screen, one or more textview and buttons on it... How to overlay a button programmically?
请参阅我的另一个答案如何以编程方式在当前活动之上显示叠加布局。Activity 的 layout.xml 不需要知道任何关于覆盖皮肤的信息。您可以放置半透明覆盖,仅覆盖屏幕的一部分,一个或多个文本视图和按钮... 如何以编程方式覆盖按钮?
- create res/layout/paused.xml RelativeLayout template or use any layout toplevel
- create a function to show overlay skin
- key is to get handle to layout.xml, use LayoutInflater class to parse xml to view object, add overlay view to current layout structure
- My example uses a timer to destroy overlay object by completely removing it from the view structure. This is probably what you want as well to get rid of it without a trace.
- 创建 res/layout/paused.xml 相对布局模板或使用任何布局顶层
- 创建一个函数来显示覆盖皮肤
- 关键是获取layout.xml的句柄,使用LayoutInflater类解析xml查看对象,添加overlay view到当前布局结构
- 我的示例使用计时器通过将覆盖对象从视图结构中完全移除来销毁它。这可能也是你想要的,让它无影无踪。
My goal was that main activities are not aware of any overlay skin, overlays come and go, many different overlays, still able to use overlay1.xml text files as a template, and content should programmatically be updated. I do pretty much what CommonsWare told us my post shows the actual program code to get started.
我的目标是主要活动不知道任何覆盖皮肤,覆盖层来来去去,许多不同的覆盖层,仍然能够使用overlay1.xml 文本文件作为模板,并且应该以编程方式更新内容。我几乎按照 CommonsWare 告诉我们的方式去做,我的帖子展示了开始使用的实际程序代码。
disclaimer: OPs "Thanks for your input. This is how I pictured it being done. I have to give credit to the answer below" comment does not mean my answer but CommonsWare answer. Stackoverflow have changed post orderings.
免责声明:OPs“感谢您的输入。这就是我想象它完成的方式。我必须相信下面的答案”评论并不意味着我的答案,而是 CommonsWare 的答案。Stackoverflow 更改了发布顺序。