android自定义底部弹出对话框

现在很多应用的头像设置都要求从屏幕底部弹出对话框,因为Android系统自带的对话框都是在屏幕中央,所以对于显示在底部的对话框就需要自己定义了。

代码如下:

package com.org.buttomdialog;

import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {
	private Dialog setHeadDialog;
	private View mDialogView;
	private Button mButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mButton = (Button) findViewById(R.id.button1);

		mButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				showDialog();
			}
		});
	}

	public void showDialog() {
		setHeadDialog = new Builder(this).create();
		setHeadDialog.show();
		mDialogView = View.inflate(getApplicationContext(),
				R.layout.layout_dialog, null);

		setHeadDialog.getWindow().setContentView(mDialogView);

		WindowManager windowManager = this.getWindowManager();
		Display display = windowManager.getDefaultDisplay();
		WindowManager.LayoutParams lp = setHeadDialog.getWindow()
				.getAttributes();
		lp.width = (int) (display.getWidth()); // 设置宽度
		setHeadDialog.getWindow().setAttributes(lp);
		bindDialogEvent();

	}

	private void bindDialogEvent() {
		Button cameraButton = (Button) mDialogView
				.findViewById(R.id.iv_userinfo_takepic);
		Button photoButton = (Button) mDialogView
				.findViewById(R.id.iv_userinfo_choosepic);
		Button cancelButton = (Button) mDialogView
				.findViewById(R.id.iv_userinfo_cancle);

		cameraButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				setHeadDialog.dismiss();
			}
		});
		photoButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				setHeadDialog.dismiss();
			}
		});
		cancelButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				setHeadDialog.dismiss();
			}
		});
	}
}
自定义布局:layout_dialog.xml




    

        

            
对于Button的选择效果的xml文件就不贴出了。

运行效果如下:

android自定义底部弹出对话框_第1张图片

你可能感兴趣的:(android,对话框,android开发)