1.
package com.example.gsc.popwindowdemo; import android.app.Dialog; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.DialogFragment; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; /** * Created by qundui on 2017/6/11. */ public class Mydiolagfremnt extends DialogFragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); ListView lv=new ListView(getContext()); String[] mdatas={"a","b","c"}; lv.setAdapter(new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1,android.R.id.text1,mdatas)); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //popupWindow.dismiss(); Mydiolagfremnt.this.dismiss(); } }); return lv; } @Override public void onResume() { WindowManager.LayoutParams a=new WindowManager.LayoutParams();// 必须放在setcontentview ( 创建以后) 之后否则无效 a.width=200; a.height=600; getDialog().getWindow().setAttributes(a); getDialog().getWindow().setGravity(Gravity.BOTTOM);//显示位置必须在设置宽高以后否则无效 super.onResume(); } @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { return super.onCreateDialog(savedInstanceState); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); }}
2. act中想使用fragment 一样玩 dialog
Mydiolagfremnt m=new Mydiolagfremnt(); m.show(getSupportFragmentManager(),"ass");