作者:重新生活好吗 | 来源:互联网 | 2023-08-29 09:31
现在很多应用的头像设置都要求从屏幕底部弹出对话框,因为Android系统自带的对话框都是在屏幕中央,所以对于显示在底部的对话框就需要自己定义了。代码如下:packagecom.org.buttomd
现在很多应用的头像设置都要求从屏幕底部弹出对话框,因为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
android:layout_
android:layout_
android:orientation="vertical" >
android:layout_
android:layout_
android:layout_alignParentBottom="true"
android:background="#000000"
android:gravity="center_horizontal"
android:orientation="vertical" >
android:layout_
android:layout_
android:background="#ffffff"
android:gravity="center"
android:orientation="vertical"
android:padding="7dp" >
android:layout_
android:layout_
android:background="#ffffff"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="7dp" >
android:layout_
android:layout_
android:background="#ffffff"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="7dp" >
对于Button的选择效果的xml文件就不贴出了。
运行效果如下: