自訂的載入彈窗

來源:互聯網
上載者:User

標籤:

Android資料載入的時候,往往需要一個等待彈窗。而系統內建的彈窗樣式往往和我們軟體不想符合,這些記錄一個,簡單的自訂樣式的載入等待框。(用到的所有素材我會在分享給大家)

先上

1、建立自訂配置樣式 loading_dialog.xml。

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:id="@+id/dialog_view" 4     android:orientation="vertical" 5     android:layout_width="match_parent" 6     android:layout_height="match_parent" 7     android:minHeight="80dp" 8     android:minWidth="200dp" 9     android:gravity="center"10     android:background="@drawable/corners_5"11     android:padding="10dp">12     <ImageView13         android:id="@+id/img"14         android:layout_width="wrap_content"15         android:layout_height="wrap_content"16         android:src="@drawable/ll_loading_outside"17         android:layout_marginBottom="10dp"18         />19     <TextView20         android:id="@+id/tipTextView"21         android:layout_width="wrap_content"22         android:layout_height="wrap_content"23         android:text="資料載入中……" />24 </LinearLayout>
View Code

2、建立樣式屬性檔案 corners_5.xml。

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <!-- 填充色 -->    <solid android:color="#fff" />    <!-- 邊距 -->    <corners        android:topLeftRadius="5dp"        android:topRightRadius="5dp"        android:bottomLeftRadius="5dp"        android:bottomRightRadius="5dp"/></shape>
View Code

3、在styles.xml 下添加自訂樣式 

    <!-- 自訂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">@color/colorEmpty</item>        <item name="android:windowIsFloating">true</item>        <item name="android:windowContentOverlay">@null</item>    </style>
View Code

4、在colors.xml 定義一個空白的顏色樣式

<color name="colorEmpty">#00000000</color><!-- 透明色-->
View Code

5、在res檔案夾下建立檔案夾anim 並在該檔案夾下建立彈窗動畫效果 loadingdialog_anim.xml

<?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> 
View Code

6、下面我們自訂一個協助類 LoadingDialog。

package until;import android.app.Dialog;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import com.lyf.net.R;/** * lyf on 2016/5/14. * 顯示等待彈窗 */public class LoadingDialog {    public static Dialog  getLoadingDialog(Context context,String msg) {        LayoutInflater inflater = LayoutInflater.from(context);        View v = inflater.inflate(R.layout.loading_dialog, null);// 得到載入view        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 載入布局        // main.xml中的ImageView        ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);        TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字        // 載入動畫        Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(context, R.anim.loadingdialog_anim);        // 使用ImageView顯示動畫        spaceshipImage.startAnimation(hyperspaceJumpAnimation);        tipTextView.setText(msg);// 設定載入資訊        Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);// 建立自訂樣式dialog        loadingDialog.setCancelable(false);// 不可以用“返回鍵”取消        loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(                LinearLayout.LayoutParams.MATCH_PARENT,                LinearLayout.LayoutParams.MATCH_PARENT));// 設定布局        return loadingDialog;    }}
View Code

這樣我們的自訂的Dialog彈窗就結束了,下面是調用。

Dialog pd = LoadingDialog.getLoadingDialog(AddXF_InfoActivity.this,"載入中...");
pd.show();

關閉

pd.dismiss();

素材:https://yunpan.cn/cSZ58jgIGcGXH  訪問密碼 a13e。


自訂的載入彈窗

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.