1.自定义Dialog 弹出
noCardDialog=new NoCardDialog(context);
Window noCardDialogWindow=noCardDialog.getWindow();
WindowManager.LayoutParams lp=noCardDialogWindow.getAttributes();
noCardDialogWindow.setGravity(Gravity.CENTER);
lp.width=300;
noCardDialogWindow.setAttributes(lp);
noCardDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
noCardDialog.show();
class NoCardDialog extends Dialog {
private ImageView iv1;
private ImageButton ib1;
public NoCardDialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setCancelable(false);// 设置点击屏幕Dialog不消失
setContentView(R.layout.nobindcard);
iv1= (ImageView) findViewById(R.id.nobindcard_iv1);
ib1= (ImageButton) findViewById(R.id.nobindcard_ib1);
if(mark==1){
iv1.setImageResource(R.mipmap.nobind_topup);
}else if(mark==2){
iv1.setImageResource(R.mipmap.nobind_withdrawal);
}
}
}
2.PopupWindow
public class PopMenuView implements OnItemClickListener {
private Context context;
private PopupWindow popupWindow;
private ListView listView;
private OnPItemClickListener listener;
private LayoutInflater inflater;
private int thisPosition;
private Handler handler = new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case 0:
if(listener != null) listener.onItemClick(thisPosition);
break;
case 1:
if(listener != null) listener.onDialogDismiss();
break;
default:
break;
}
}
};
public PopMenuView(Context context) {
this.context = context;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = (View) inflater.inflate(R.layout.searchpopmenu, null);
listView = (ListView) view.findViewById(R.id.searchpopmenu_listView);
listView.setAdapter(new PopMenuAdapter(context));
listView.setOnItemClickListener(this);
popupWindow = new PopupWindow(view, context.getResources()
.getDimensionPixelSize(R.dimen.popmenu_width),
LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
popupWindow.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
// TODO Auto-generated method stub
handler.sendEmptyMessage(1);
}
});
};
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
thisPosition = position;
handler.sendEmptyMessage(0);
dismiss();
}
public void setOnItemClickListener(OnPItemClickListener listener) {
this.listener = listener;
}
public void showAsDropDown(View parent) {
popupWindow.showAsDropDown(parent, -10, context.getResources()
.getDimensionPixelSize(R.dimen.popmenu_yoff));
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.update();
}
public void dismiss() {
if (popupWindow.isShowing())
popupWindow.dismiss();
}
public interface OnPItemClickListener {
public void onItemClick(int index);
public void onDialogDismiss();
}
}
3.设置背景为透明的方法
把PopWindow自定义的layout背景色设置为透明的颜色
<solid android:color="#80000000"/>
前两位为透明度
4.7种形式的Android Dialog使用举例
http://www.oschina.net/question/54100_32486
5.Service中弹出Dialog对话框,即全局性对话框
先说具体做法,原因在其后给出:
写好Alter功能块后,在alter.show()语句前加入:
alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
注:alter为AlertDialog类型对象
然后在AndroidManifest.xml中加入权限:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"></uses-permission>
下面进行简单的解释:
如果只在Service中写入常在Activity中使用的创建Alter的代码,运行时是会发生错误的,因为Alter的显示需要依附于一个确定的Activity类。
而以上做法就是声明我们要弹出的这个提示框是一个系统的提示框,即全局性质的提示框,
所以只要手机处于开机状态,无论它现在处于何种界面之下,只要调用alter.show(),就会弹出提示框来