1. 程式人生 > >android載入動畫,不佔用資源

android載入動畫,不佔用資源

  protected AlertDialog alertDialog=null;
    
    public void lock(){
        if(alertDialog==null){
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setCancelable(false);
            alertDialog=builder.create();
            alertDialog.show();
            Window window = alertDialog.getWindow();//得到AlertDialog的Window
            window.setContentView(R.layout.lock);//給Window設定自定義佈局
        }else {
            alertDialog.show();
        }

    }
  public void unLock(){
        if(alertDialog != null){
            alertDialog.dismiss();
        }
    }

佈局檔案

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical">

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
使用AlertDialog載入自定義佈局完成,很好用而且不會佔用很多資源