歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android中自定義的加載對話框和加載條

Android中自定義的加載對話框和加載條

日期:2017/3/1 10:33:11   编辑:Linux編程

先分享一個常用的轉動形式加載對話框。


這個是很早前一個應用,一哥們寫的控件。

後來發現聯想的應用中基本所用應用加載框都是這個。(開源代碼沒版權一說吧)

控件比較簡單,分享下思路:

1.首先這是一個自定義的dialog,重寫了dialog,系統的progressdialog也是繼承了dialog。

[java]

  1. /**
  2. * @author Nono
  3. *
  4. */
  5. public class CustomProgressBarDialog extends Dialog {
  6. private LayoutInflater inflater;
  7. private Context mContext;
  8. private LayoutParams lp;
  9. /**
  10. * @param context
  11. */
  12. public CustomProgressBarDialog(Context context) {
  13. super(context, R.style.NoTitleDialog);
  14. this.mContext = context;
  15. inflater = (LayoutInflater) mContext
  16. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  17. layout = inflater.inflate(R.layout.custom_progressbar, null);
  18. setContentView(layout);
  19. // 設置window屬性
  20. lp = getWindow().getAttributes();
  21. lp.gravity = Gravity.CENTER;
  22. lp.dimAmount = 0; // 去背景遮蓋
  23. lp.alpha = 1.0f;
  24. getWindow().setAttributes(lp);
  25. }
  26. }
2.主要是setContentView(view)中的這個view該如何定義。

[java]

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal" android:layout_width="fill_parent"
  4. android:layout_height="wrap_content" android:gravity="center">
  5. <ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
  6. android:layout_width="wrap_content" android:layout_height="wrap_content"
  7. style="@style/CustomProgessBarStyle" android:padding="10dip"
  8. android:layout_gravity="center" android:gravity="center"
  9. />
  10. <TextView android:id="@+id/load_info_text" android:text="@string/loading" android:layout_width="wrap_content"
  11. android:layout_height="wrap_content" android:textColor="#FFFFFF"
  12. android:padding="10dip" />
  13. </LinearLayout>
3.上面的核心代碼就是那個style,下面我們看下這個style代碼;

[html]

  1. <style name="CustomProgessBarStyle">
  2. <item name="android:indeterminateDrawable">@drawable/custom_progress_bar</item>
  3. <item name="android:minWidth">50dip</item>
  4. <item name="android:maxWidth">50dip</item>
  5. <item name="android:minHeight">50dip</item>
  6. <item name="android:maxHeight">50dip</item>
  7. </style>
4.我們看第一個item,也就是不穩定的圖片,也是關鍵代碼。自定義的drawable,我們知道res下的drawable文件中可以定義多種樣式的drawable資源文件,
Copyright © Linux教程網 All Rights Reserved