欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義ProgressDialog進(jìn)度等待框

 更新時(shí)間:2020年05月19日 10:13:42   作者:無(wú)緣公子  
這篇文章主要介紹了Android自定義ProgressDialog進(jìn)度等待框,通過(guò)本文大家可以嘗試?yán)肁ndroid自定義ProgressDialog,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Android本身已經(jīng)提供了ProgressDialog進(jìn)度等待框,使用該Dialog,我們可以為用戶(hù)提供更好的體驗(yàn):在網(wǎng)絡(luò)請(qǐng)求時(shí),彈出此框等待網(wǎng)絡(luò)數(shù)據(jù)。 不過(guò),既然是為了提高用戶(hù)體驗(yàn),我們肯定希望該Dialog能更加炫酷,讓用戶(hù)看著更舒服。那如何做呢,當(dāng)然是我們自己定義一個(gè)ProgressDialog了。
可以先看下,接下來(lái)將實(shí)現(xiàn)的Dialog效果圖:

步驟1:要定義布局文件,該布局文件即是Dialog的布局了

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@+id/dialog_view" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:background="@drawable/dialog_load_bg" 
 android:gravity="center" 
 android:minHeight="100dp" 
 android:minWidth="190dp" 
 android:orientation="vertical" 
 android:padding="10dp" > 
 
 <ImageView 
  android:id="@+id/img" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:src="@drawable/publicloading" /> 
 
 <TextView 
  android:id="@+id/tipTextView" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_marginLeft="10dp" 
  android:textColor="#acacac" 
  android:textSize="15sp" /> 
 
</LinearLayout> 

在布局文件中,我們只定義了兩個(gè)組件,一個(gè)ImageView,用于顯示旋轉(zhuǎn)圖,一個(gè)TextView,用于顯示消息文本

步驟2:定義動(dòng)畫(huà),使得彈出框上的圖片可以不停的旋轉(zhuǎn)。

<?xml version="1.0" encoding="utf-8"?> 
<set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android"> 
 <rotate 
  android:interpolator="@android:anim/linear_interpolator" 
  android:pivotX="50%" 
  android:pivotY="50%" 
  android:fromDegrees="0" 
  android:toDegrees="+360" 
  android:duration="1500" 
  android:startOffset="-1" 
  android:repeatMode="restart" 
  android:repeatCount="-1"/> 
</set> 

步驟3:實(shí)現(xiàn)自定義的Dialog邏輯

/** 
 * 公用的彈出框 
 * 
 * @author lining 
 */ 
public class LoadingDialog { 
 
 /** 
  * 得到自定義的progressDialog 
  * 
  * @param context 
  * @param msg 
  * @return 
  */ 
 public static Dialog createLoadingDialog(Context context, String msg) { 
 
  // 首先得到整個(gè)View 
  View view = LayoutInflater.from(context).inflate( 
    R.layout.loading_dialog_view, null); 
  // 獲取整個(gè)布局 
  LinearLayout layout = (LinearLayout) view 
    .findViewById(R.id.dialog_view); 
  // 頁(yè)面中的Img 
  ImageView img = (ImageView) view.findViewById(R.id.img); 
  // 頁(yè)面中顯示文本 
  TextView tipText = (TextView) view.findViewById(R.id.tipTextView); 
 
  // 加載動(dòng)畫(huà),動(dòng)畫(huà)用戶(hù)使img圖片不停的旋轉(zhuǎn) 
  Animation animation = AnimationUtils.loadAnimation(context, 
    R.anim.dialog_load_animation); 
  // 顯示動(dòng)畫(huà) 
  img.startAnimation(animation); 
  // 顯示文本 
  tipText.setText(msg); 
 
  // 創(chuàng)建自定義樣式的Dialog 
  Dialog loadingDialog = new Dialog(context, R.style.loading_dialog); 
  // 設(shè)置返回鍵無(wú)效 
  loadingDialog.setCancelable(false); 
  loadingDialog.setContentView(layout, new LinearLayout.LayoutParams( 
    LinearLayout.LayoutParams.MATCH_PARENT, 
    LinearLayout.LayoutParams.MATCH_PARENT)); 
 
  return loadingDialog; 
 } 
} 

代碼注釋已經(jīng)很詳細(xì)了,有一處需要注意的,就是在創(chuàng)建Dialog實(shí)例時(shí),需要傳遞一個(gè)theme,該theme是Dialog的風(fēng)格:

<!-- 自定義loading dialog --> 
;style name="loading_dialog" parent="android:style/Theme.Dialog"> 
 <item name="android:windowFrame">@null</item> 
 <item name="android:windowNoTitle">true</item> 
 <item name="android:windowBackground">@drawable/dialog_load_bg</item> 
 <item name="android:windowIsFloating">true</item> 
 <item name="android:windowContentOverlay">@null</item> 
;/style> 

步驟4:使用自定義的ProgressDialog
接下來(lái),我們可以直接使用已經(jīng)定義好的Dialog了,很簡(jiǎn)單,只需要將Dialog顯示和關(guān)閉即可,建議將講方法封裝起來(lái),放在

BaseActivity(基類(lèi))中,方便隨時(shí)調(diào)用。
/** 
 * 顯示Dialog 
 */ 
private void showDialog() { 
 if (dialog == null) { 
  dialog = LoadingDialog.createLoadingDialog(this, "正在加載中..."); 
  dialog.show(); 
 } 
} 
 
/** 
 * 關(guān)閉Dialog 
 */ 
private void closeDialog() { 
 if (dialog != null) { 
  dialog.dismiss(); 
  dialog = null; 
 } 
} 

通過(guò)上面步驟,我們即完成了自定義的ProgressDialog,當(dāng)然,具體在項(xiàng)目中需要什么樣的效果,可以調(diào)整。

相關(guān)文章

最新評(píng)論