如何在一些Android应用和ICS中看到创建帮助覆盖?

ssu*_*z28 92 android

我想创建帮助叠加层,就像你第一次加载ICS时看到的那样,或者在ES File Explorer或Apex Launcher等应用程序中加载(有更多,但我现在想不到它们).这只是一个相对布局,一个视图位于另一个视图的顶部吗?我无法找到任何用于执行此类操作的示例代码.任何人都知道这是如何完成或有任何想法?

ES文件浏览器 ES文件浏览器

Com*_*are 84

我们假设您通常会打电话setContentView(R.layout.main),但在第一次运行时,您希望拥有此叠加层.

步骤1:创建一个FrameLayoutJava代码并将其传递给setContentView().

第二步:LayoutInflater用来膨胀R.layout.mainFrameLayout.

步骤#3:用于LayoutInflater将叠加层膨胀到FrameLayout.

步骤#4:当用户点击按钮(或其他)以关闭覆盖图时,请致电removeView()以从中移除覆盖图FrameLayout.

由于叠加层是后来的子窗口FrameLayout,它将浮动在内容的顶部R.layout.main.

  • @ ssuperz28:是的,除了手绘的箭头和泡泡.你自己就是那些人.:-) (6认同)
  • @ user3364963:动态调整事物的位置.我的回答有些简化了.我假设他们确定要突出显示的元素的位置并调整"教练标记"的坐标."coach marks"图层的自定义`ViewGroup`可以处理它,并且有一些库(例如,`ShowcaseView`)提供该模式的固定实现. (2认同)

Ode*_*ner 80

用户体验谈话中的"教练标记"是"帮助叠加":-)

coach_mark.xml是您的教练标记布局

coach_mark_master_viewcoach_mark.xml中最顶层视图(root)的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();
}
Run Code Online (Sandbox Code Playgroud)

添加coach_mark.xml的样本(由Oded Breiner给出的这个优秀的解决方案),因此很容易让ppl复制和粘贴以快速查看工作示例.

这里是coach_mark.xml的示例,将 - > drawable/coach_marks更改为您的图像:

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>
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

  • 对于那些不知道如何设置对话框主题的人,只需实例化对话框:`new Dialog(getContext(),R.style.WalkthroughTheme);` (3认同)