我如何创建一个帮助覆盖,就像你在一些Android应用程序和ICS中看到的那样?

我如何创建一个帮助覆盖,就像你在一些Android应用程序和ICS中看到的那样?,android,Android,我想创建帮助覆盖,就像你第一次加载ICS时看到的,或者在ES File Explorer或Apex Launcher等应用程序中看到的(还有更多,但我现在想不起来)。这只是一个相对布局,一个视图位于另一个视图之上吗?我还没有找到做这种事情的任何示例代码。有人知道这是怎么做到的或者有什么想法吗 你可以很快做到。例如,您添加了一个线性布局,其中您放置了一张带有alpha的图片,该图片与您的帮助信息相对应,并且您希望绘制什么样的覆盖图。在活动的xml中,您将此布局放在活动布局之后的RelativeL

我想创建帮助覆盖,就像你第一次加载ICS时看到的,或者在ES File Explorer或Apex Launcher等应用程序中看到的(还有更多,但我现在想不起来)。这只是一个相对布局,一个视图位于另一个视图之上吗?我还没有找到做这种事情的任何示例代码。有人知道这是怎么做到的或者有什么想法吗


你可以很快做到。例如,您添加了一个线性布局,其中您放置了一张带有alpha的图片,该图片与您的帮助信息相对应,并且您希望绘制什么样的覆盖图。在活动的xml中,您将此布局放在活动布局之后的RelativeLayout中,该布局的可见性已消失。当您想要绘制帮助信息时,只需将此可见性设置为可见即可


我希望,我很清楚,如果您有任何问题,我很乐意回答。

让我们假设您通常会调用
setContentView(R.layout.main)
,但在第一次运行时,您希望使用此覆盖

步骤1:在Java代码中创建一个
FrameLayout
,并将其传递给
setContentView()

步骤2:使用
LayoutInflater
R.layout.main
充气到
FrameLayout

步骤3:使用
LayoutInflater
将覆盖层充气到
FrameLayout

步骤4:当用户点击按钮(或其他任何按钮)关闭覆盖时,调用
removeView()
FrameLayout
中删除覆盖


由于覆盖是
FrameLayout
的后续子项,因此它将浮动在
R.layout.main的内容上方。请参见我的另一个答案,即如何以编程方式在当前活动上方显示覆盖布局。Activity的layout.xml不需要了解任何关于覆盖外观的信息。你可以把覆盖半透明,只覆盖部分屏幕,一个或多个文本视图和按钮上。。。

  • 创建res/layout/paused.xml RelativeLayout模板或使用任何布局顶层
  • 创建一个显示覆盖蒙皮的函数
  • 关键是获得layout.xml的句柄,使用LayoutInflater类解析xml以查看对象,将覆盖视图添加到当前布局结构中
  • 我的示例使用计时器将覆盖对象从视图结构中完全删除,从而销毁覆盖对象。这可能是你想要的,也就是不留痕迹地把它处理掉
我的目标是,主要活动不知道任何覆盖外观,覆盖来来去去,许多不同的覆盖,仍然能够使用overlay1.xml文本文件作为模板,内容应该以编程方式更新。我做的和Commonware告诉我们的差不多,我的帖子展示了实际的程序代码


免责声明:OPs“感谢您的输入。这是我所想象的。我必须承认下面的答案”评论不是指我的答案,而是指公共软件的答案。Stackoverflow已更改后期订单。

在UX talk中,“Coach mark”是“Help overlay:-)

coach_mark.xml是您的coach mark布局

coach\u mark\u master\u viewcoach\u 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();
}
<?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>
添加coach_mark.xml的示例(到Oded Breiner提供的这个优秀解决方案中),这样ppl就可以轻松地复制和粘贴以快速查看工作示例

coach_mark.xml示例在此处,将->可绘制/coach_标记更改为您的图像:

coach_-mark.xml

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();
}
<?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>

还可以选择使用此主题删除填充:

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

真的
@android:彩色/透明
@空的
真的
假的

感谢您的输入。这就是我想象中的情况。我不得不承认下面的答案,因为这是我要建立它的方式。它似乎更容易管理,我不必改变我的主要活动布局。@ssuperz28:是的,好吧,除了手绘箭头和气泡。这些都是你自己的事。:-)我不知道你是否做到了你想要的,但这里有另一个选择。如果您坚持Commonware给出的解决方案,那么设置箭头时唯一发生的事情就是使用resourceImage或backgroundImage定义指令的框架布局,该图像已经带有箭头(和透明度)。有没有一种方法可以做到这一点,但要相对于位于FrameLayout下方的孩子们的位置进行操作?@Commonware-先生,无论是哪种设备,这都会起作用?@user3364963:动态调整事物的位置。我的回答有些简单。我假设它们确定要高亮显示的元素的位置,并调整“coach标记”的坐标。“coach marks”层的自定义
ViewGroup
可以处理此问题,并且有一些库(例如,
ShowcaseView
)提供该模式的固定实现。您好,您如何扩展此对话框以匹配屏幕?在文档中,
对话框是一个提示[…]的小窗口。对话框不会填满屏幕,通常用于模式事件[…]。
。如果我使用这段代码,我会将主视图压缩到屏幕的一小部分。如果我设置了
android:layout\u width=“640dp”android:layout\u height=“480dp”
(即我正在测试的屏幕的实际值),它会正常工作。(当然这不是一个可行的解决方案)@ocramot:上面的例子应该是全屏的,因为匹配父项和窗口。功能\u否\u标题如果一定有其他问题,因为我以不同的方式看待它,我知道这至少有几个月了,但是这个全屏问题解决了吗?对于那些不知道如何设置对话框主题的人,只需像这样实例化对话框:
newdialog(getContext(),R.style.WalkthroughTheme)