android片段里面的popwindow

时间:2016-06-06 15:04:27

标签: android layout fragment popupwindow

我有一个片段活动,片段里面有按钮,点击它我想显示一个带有自定义布局的弹出窗口。 enter image description here

这是片段代码示例

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

与教程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;
    }
}