Android自定义弹窗效果

Android的弹窗效果有很多种,就最简单而言,就可以调用一个AlertDialog弹窗显示,可是要自定义弹窗效果有以下这种方法,就我个人而言感觉挺方便的,适用性也挺广的。

首先先简单写个AlertDialog的使用

public void showDialog(){
			AlertDialog dialog = new AlertDialog.Builder(this)
			.setTitle("提示")
			.setMessage(getResources().getString("你确定退出吗?")
			.setPositiveButton("取消", new OnClickListener() {
			
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			}).setNegativeButton("确定", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			}).create();
			dialog.show();	
		}	

	}



接下来就是自定义弹窗效果图:

Android自定义弹窗效果_第1张图片


Android自定义弹窗效果_第2张图片



下面是示例代码:



    
        

        



public class SelectPopupWindow extends PopupWindow {
	Button takePhoto;
	Button selectFromAlbum;
	Button cancel;
	View menuView;
	public SelectPopupWindow(Context ctx, OnClickListener onClickListener) {
		LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		menuView = inflater.inflate(R.layout.modify_head, null); //可以采用各种样式,自定义view
		takePhoto = (Button) menuView.findViewById(R.id.takephoto);
		takePhoto.setOnClickListener(onClickListener);
		selectFromAlbum = (Button) menuView.findViewById(R.id.selectfromalbum);
		selectFromAlbum.setOnClickListener(onClickListener);		
		cancel = (Button) menuView.findViewById(R.id.cancel);
		cancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dismiss();
			}
		});
		
		setContentView(menuView);
		setWidth(LayoutParams.MATCH_PARENT);
		setHeight(LayoutParams.WRAP_CONTENT);
		setFocusable(true);   //弹出窗体后可点击
		
		//setAnimationStyle(R.style.AnimBottom);   //设置动画
		menuView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				int height = menuView.findViewById(R.id.top).getTop();
				Log.e("huang", " top height:"+height+" y:"+event.getY());
				int y = (int) event.getY();
				if(y


public class TestPopupWindow extends Activity {
	private SelectPopupWindow popupWindow;
	private ImageView image;
	private Button btn, btn1;
	private Uri fileUri;     
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main1);
		init();
	}
	
	private void init(){
		image = (ImageView) findViewById(R.id.image);
		btn = (Button) findViewById(R.id.btn);
		btn.setOnClickListener(onClickListener);
		btn1 = (Button) findViewById(R.id.btn1);
		btn1.setOnClickListener(onClickListener);
		
	}
	
	View.OnClickListener onClickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch(v.getId()){
			case R.id.btn:
				test();
				break;
			case R.id.btn1:
				startActivity(new Intent(TestPopupWindow.this, TestGsonActivity.class));
				break;
			case R.id.takephoto:
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
				startActivityForResult(intent, 100);
				break;
			case R.id.selectfromalbum:
				Intent intent1 = new Intent(Intent.ACTION_PICK);
				intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
				startActivityForResult(intent1, 200);
				break;
				
			}
		}
	};
	
	public void test(){
		popupWindow = new SelectPopupWindow(this, onClickListener);
		popupWindow.showAtLocation(findViewById(R.id.main), 
				Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
	}
	


你可能感兴趣的:(Android)