在DialogFragment之上展示ShowView

时间:2014-05-27 18:38:47

标签: android android-dialogfragment showcaseview

我在Android应用中使用ShowcaseView。我希望能够在ShowcaseView之上显示DialogFragment。但对我来说,ShowcaseView显示在DialogFragment下方。

这个项目在github上有关于这个主题的公开问题。但我想我会按照建议在这里问。 This帖提到他解决了“使用虚假活动作为对话片段”的问题。但我不确定这意味着什么。也许这意味着他没有使用DialogFragment,而是使用Activity并将其显示为DialogFragment?我知道您可以将活动的主题设置为清单中的对话:android:theme="@android:style/Theme.Dialog"

其他github问题是herehere

我正在使用ShowcaseView的“遗留”版本:

ViewTarget target = new ViewTarget(R.id.imageView, getActivity());
ShowcaseView.insertShowcaseView(target, getActivity(), R.string.showcase_title, R.string.showcase_details);

2 个答案:

答案 0 :(得分:1)

为了让ShowcaseView能够在DialogFragment上正确显示,我们必须将DialogFragment显示为Fragment,而不是将DialogFragment显示为Dialog DialogFragment

public static class MyDialogFragment extends DialogFragment { public static MyDialogFragment newInstance() { return new MyDialogFragment(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.hello_world, container, false); View tv = v.findViewById(R.id.text); ((TextView)tv).setText("This is an instance of MyDialogFragment"); return v; } 中,我们可以这样:

public class MyDialogActivity extends Activity{

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contact_manager);
    if (savedInstanceState == null) {
        MyDialogFragment fragment = MyDialogFragment.newInstance();
        getSupportFragmentManager()
            .beginTransaction()
            .add(R.id.frameLayout, fragment, "Some_tag")
            .commit();
    }
}

我们可以在这样的活动中展示它:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />

activity_contact_manager.xml布局可以是:

<activity
    android:name=".activity.MyDialogActivity"
    android:theme="@android:style/Theme.Dialog" />

AndroidManifest.xml条目可以是:

{{1}}

答案 1 :(得分:0)

您应该编写添加方法,将Activity更改为DialogFragment

private void show(DialogFragment dialog) {
    ((ViewGroup) dialog.getDialog().getWindow().getDecorView()).addView(this);
}

它为我工作。