这是一种方法,是我觉得简单易懂代码量较少的一种:
/* 创建AlertDialog对象并显示 */
final AlertDialog alertDialog = new AlertDialog.Builder(LoginActivity.this).create();
alertDialog.show();
/* 添加对话框自定义布局 */
alertDialog.setContentView(R.layout.dialog_login);
/* 获取对话框窗口 */
Window window = alertDialog.getWindow();
/* 设置显示窗口的宽高 */
window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
/* 设置窗口显示位置 */
window.setGravity(Gravity.CENTER);
/* 通过window找布局里的控件 */
window.findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("log", "进入onclick函数体内");
// 隐藏对话框
alertDialog.dismiss();
//自己进行其他的处理
}
});
R.layout.dialog_login.xml (自己想要的样子需要在布局中设置好)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="@drawable/dialog_corner_bg"
android:layout_margin="20dp"
xmlns:android="http://schemas.android.com/apk/res/android"> <Button
android:padding="5dp"
android:layout_marginBottom="5dp"
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@null"
android:text="OK"
android:textColor="@color/btn_txt_blue"
android:textSize="25sp"
android:textStyle="bold" /> </LinearLayout>
dialog_corner_bg.xml (对话框的背景)
<?xml version="1.0" encoding="utf-8"?>
<!-- 用于设置信息对话框的圆角 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="20dip"></corners>
<solid android:color="@color/white"></solid>
</shape>
效果如下图:
************************************************************************************************
这是另外一种方法:(需要设置合适的主题)
final Dialog dialog2 = new Dialog(LoginActivity.this, R.style.ActionSheetDialogStyle);
LinearLayout view = (LinearLayout) LayoutInflater.from(LoginActivity.this).inflate(R.layout.dialog_forgotpwd, null);
TextView dismiss = (TextView) view.findViewById(R.id.btn_dismiss);
TextView forgotPin = (TextView) view.findViewById(R.id.btn_forgotpin);
TextView forgotPassword = (TextView) view.findViewById(R.id.btn_forgotpassword);
dismiss.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog2.dismiss();
}
});
forgotPin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { }
});
forgotPassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { }
}); dialog2.setContentView(view);
Window dialogWindow = dialog2.getWindow();
dialogWindow.setGravity(Gravity.BOTTOM);
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.y = 30; //距离底部的高度
dialogWindow.setAttributes(lp);
dialog2.show();
style.xml
<style name="ActionSheetDialogStyle" parent="@android:style/Theme.Dialog">
<!-- 背景透明 -->
<item name="android:windowBackground">@drawable/email_dialogfrag_bg</item>
<item name="android:windowContentOverlay">@null</item>
<!-- 浮于Activity之上 -->
<item name="android:windowIsFloating">true</item>
<!-- 边框 -->
<item name="android:windowFrame">@null</item>
<!-- Dialog以外的区域模糊效果 -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 无标题 -->
<item name="android:windowNoTitle">true</item>
<!-- 半透明 -->
<item name="android:windowIsTranslucent">true</item>
<item name="android:color">@color/white</item>
</style>
效果如下图: