从Fragment调用DialogFragment(不是FragmentActivity)? [英] Calling DialogFragment from Fragment (not FragmentActivity)?
问题描述
我确实有一个FragmentActivity,其中包含一个Fragment列表(带有在它们之间导航的方法). 在那些片段之一中,我需要调用DialogFragment来显示"zoom".在该片段中包含的图片上.
I do have a FragmentActivity which contains a Fragment list (with methods to navigate between them). In one of those fragments I need to call a DialogFragment to display a "zoom" on a Picture contained in that fragment.
但是似乎您不能直接从Fragment调用DialogFragment.
But it seems that you can't call a DialogFragment directly from a Fragment.
有没有办法得到某种回叫"?到FragmentActivity,以使其在片段上方显示DialogFragment.
Is there any way to get some kind of "callback" to the FragmentActivity to make this display the DialogFragment over the fragment.
或者只是一个小故障"直接从Fragment调用它.
Or simply a "glitch" to call it directly from the Fragment.
如果是这种情况,我有什么选择?
If that is the case what are my options?
推荐答案
创建新的Dialog
时,可以使用Fragment
中的这个(非常)简单的方法来简单地调用它.
When you create a new Dialog
, you can simply call it using this (very) simple method from a Fragment
.
DialogFragment dialog = DialogFragment.instantiate(getActivity(), "Hello world");
dialog.show(getFragmentManager(), "dialog");
如果要使用自己的对话框,请使用这种代码.
If you want to use your own dialog, please use that kind of code.
public class MyDialogFragment extends DialogFragment
{
//private View pic;
public MyDialogFragment()
{
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_my_dialog, new LinearLayout(getActivity()), false);
// Retrieve layout elements
TextView title = (TextView) view.findViewById(R.id.text_title);
// Set values
title.setText("Not perfect yet");
// Build dialog
Dialog builder = new Dialog(getActivity());
builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
builder.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
builder.setContentView(view);
return builder;
}
}
这篇关于从Fragment调用DialogFragment(不是FragmentActivity)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!