安卓 自訂AlertDialog對話方塊(載入提示框),安卓alertdialog

來源:互聯網
上載者:User

安卓 自訂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>

附:旋轉背景圖

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.