android 片段内的弹出窗口

popwindow inside android Fragment

我有一个带片段的 activity,片段内有按钮,单击该按钮我想显示一个带有自定义布局的弹出窗口 window。

这是片段代码示例

public class TabContent extends Fragment {
JSONArray jArray;
private ImageView ime;
private GridView gridView;
private GridViewAdapter gridAdapter;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.tabcontent, container, false);
    Bundle bundle = getArguments();
    gridView = (GridView) view.findViewById(R.id.gridView);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            ImageItem item = (ImageItem) parent.getItemAtPosition(position);
            try {
                final JSONObject menuData = (JSONObject) dobj.getMenuData(TabPosition,position);
                Log.i("item", String.valueOf(menuData));


                // here i want to trigger the popupwindow


            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    });


    return view;
}

如教程中所述androidbegin.com/tutorial/android-dialogfragment-tutorial

使用下方打开对话框片段

FragmentManager fm = getSupportFragmentManager();
    AlertDFragment alertdFragment = new AlertDFragment();
                    // Show Alert DialogFragment
                    alertdFragment.show(fm, "Alert Dialog Fragment");

下面是可以自定义布局的Dialog Fragment的代码,dialogfragment是可以自定义布局的XML文件

 public class DFragment extends DialogFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.dialogfragment, container,
                false);
        getDialog().setTitle("DialogFragment Tutorial");        
        // Do something else
        return rootView;
    }
}
      AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());

 LayoutInflater inflater = getActivity().getLayoutInflater();
  View customView=inflater.inflate(R.layout.yourxml, null);
   dialogBuilder.setView(customView);

      final AlertDialog alertDialog = dialogBuilder.create();
                            alertDialog.setCancelable(false);
                            alertDialog.setCanceledOnTouchOutside(false);
            //Intialize your view components here
      example: TextView txtsample=(TextView)customView.findViewById(R.id.yourtxtviewid);



     alertDialog.show();