说明:大部分内容都是参考别的文章,这里做整理是为了以后的编程有实用的模板,可以即需即用。
一、自定义 UI 的 Dialog
代码:
private void showDialog() {
final Dialog dialog = new Dialog(this, R.style.NormalDialogStyle);
View view = View.inflate(this, R.layout.dialog_custom, null);
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.CENTER;
dialog.setContentView(view, lp);
// 设置点击对话框外部是否关闭对话框
dialog.setCanceledOnTouchOutside(true);
Button cancel = (Button) view.findViewById(R.id.bt_cancel);
Button confirm = (Button) view.findViewById(R.id.bt_confirm);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
styles.xml:
<!-- dialog 对话框的样式 -->
<style name="NormalDialogStyle">
<!-- 对话框背景 -->
<item name="android:windowBackground">@android:color/transparent</item>
<!-- 边框 -->
<item name="android:windowFrame">@null</item>
<!-- 没有标题 -->
<item name="android:windowNoTitle">true</item>
<!-- 是否浮现在 Activity 之上 -->
<item name="android:windowIsFloating">true</item>
<!-- 背景透明 -->
<item name="android:windowIsTranslucent">false</item>
<!-- 是否有覆盖 -->
<item name="android:windowContentOverlay">@null</item>
<!-- 进出的显示动画 -->
<!--<item name="android:windowAnimationStyle">@style/normalDialogAnim</item>-->
<!-- 背景变暗 -->
<item name="android:backgroundDimEnabled">true</item>
</style>
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_solid_r4_ffffff">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv_tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/iv_icon"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp"
android:text="提示内容" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_tip"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:gravity="center">
<Button
android:id="@+id/bt_cancel"
android:layout_width="100dp"
android:layout_height="35dp"
android:layout_marginRight="12dp"
android:background="@drawable/bg_solid_r4_959595"
android:text="@string/common_return"
android:textColor="@color/white"
android:textSize="16dp" />
<Button
android:id="@+id/bt_confirm"
android:layout_width="100dp"
android:layout_height="35dp"
android:layout_marginLeft="12dp"
android:background="@drawable/bg_solid_r4_e52848"
android:text="@string/common_confirm"
android:textColor="@color/white"
android:textSize="16dp" />
</LinearLayout>
</RelativeLayout>
二、加载中 Dialog
// BaseActivity.class
private Dialog mLoadingDialog;
protected void showLoadingDialog() {
if (mLoadingDialog == null) {
mLoadingDialog = new Dialog(this, R.style.NormalDialogStyle);
View view = LayoutInflater.from(this).inflate(R.layout.dialog_loading, null);
WindowManager.LayoutParams lp = mLoadingDialog.getWindow().getAttributes();
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.CENTER;
mLoadingDialog.setContentView(view, lp);
mLoadingDialog.setCanceledOnTouchOutside(false);
mLoadingDialog.show();
} else {
mLoadingDialog.show();
}
}
protected void hideLoadingDialog() {
if (mLoadingDialog != null) {
mLoadingDialog.dismiss();
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp">
<ProgressBar
android:id="@+id/more_progress"
style="?android:attr/progressBarStyleInverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminateDuration="1500" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/more_progress"
android:layout_centerHorizontal="true"
android:gravity="bottom|center_horizontal"
android:text="加载中..."
android:textSize="12sp"
android:visibility="gone" />
</RelativeLayout>
三、列表选择框 AlertDialog
private void showStationDialog() {
final String[] items = new String[]{"选项一", "选项二", "选项三", "选项四", "选项五"};
AlertDialog dialog = new AlertDialog.Builder(this)
.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mTvSelect.setText(items[which]);
}
}).create();
dialog.show();
}
四、定点弹窗 PopupWindow
private void showDeletePop(View v) {
View view = LayoutInflater.from(mContext).inflate(R.layout.pop_delete, null, false);
TextView tvDelete = (TextView) view.findViewById(R.id.tv_delete);
final PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
// 设置加载动画
// popupWindow.setAnimationStyle(R.anim.xxx);
// 点击非 PopupWindow区域时使 PopupWindow消失,如果为 false,无论按多少次后退键 PopupWindow都不会关闭,而且退不出程序
popupWindow.setTouchable(true);
// 要为 popupWindow设置一个背景才有效
popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
// 设置 popupWindow显示的位置,参数依次是参照 View,x轴的偏移量,y轴的偏移量
popupWindow.showAsDropDown(v, 50, -100);
tvDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
... ...
}
});
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/bg_msg">
<TextView
android:id="@+id/tv_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="删除" />
</LinearLayout>
参考文章:
1、https://blog.csdn.net/a_zhon/article/details/54578047
2、http://www.runoob.com/w3cnote/android-tutorial-popupwindow.html