安卓 自訂AlertDialog對話方塊(載入提示框),安卓alertdialog
AlertDialog有以下六種使用方法:
一、簡單的AlertDialog(只顯示一段簡單的資訊)
二、帶按鈕的AlertDialog(顯示提示資訊,讓使用者操作)
三、類似ListView的AlertDialog(展示內容)
四、類似RadioButton的AlertDialog(讓使用者選擇,單選)
五、類似CheckBox的AlertDialog(讓使用者多選)
六、自訂View的AlertDialog(當以上方式滿足不了你的需求,就要自訂了)
這裡寫的就是第六種用法,如下(效果類似與IOS中的MBProgressHUD)
具體實現如下:
.java代碼
// 自訂彈出框,框內放入圖片,圖片設定旋轉動畫
AlertDialog alert_progress = new AlertDialog.Builder(當前activity.this).create();
alert_progress.show();
alert_progress.setCancelable(false); // 點擊背景時對話方塊不會消失
// alert_progress.dismiss(); // 取消對話方塊
Window window = alert_progress.getWindow();
window.setContentView(R.layout.alert_dialog_progress_view); //載入自訂的布局檔案
WindowManager.LayoutParams wm = window.getAttributes();
wm.width = 250; // 設定對話方塊的寬
wm.height = 200; // 設定對話方塊的高
wm.alpha = 0.5f; // 對話方塊背景透明度
wm.dimAmount = 0.6f; // 遮罩層亮度
window.setAttributes(wm);
ImageView img = (ImageView)window.findViewById(R.id.progress_bar); // 擷取布局檔案中的ImageView控制項
img.setBackgroundResource(R.drawable.loading_one); // 設定圖片,也可在布局檔案中設定
// 設定旋轉動畫
Animation tranfrom = new RotateAnimation(0,359,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);(359:旋轉角度(可自調),若為360會有卡頓,正數為順勢針旋轉,負數為逆時針)
tranfrom.setDuration(2000); // 旋轉速度
tranfrom.setFillAfter(true);
tranfrom.setRepeatCount(-1); // -1為一隻旋轉,若10,則旋轉10次設定的角度後停止
// tranfrom.cancel(); // 取消動畫
img.setAnimation(tranfrom);
布局代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBlack">
<ImageView
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<TextView
android:text="正在載入..."
android:layout_width="match_parent"
android:layout_height="20dp"
android:textColor="@color/colorWhite"
android:layout_gravity="center"
android:gravity="center"/>
</LinearLayout>
附:旋轉背景圖