Android loading Dialog 自定义

来源:互联网 发布:雨燕轮毂数据 编辑:程序博客网 时间:2024/04/28 12:16

                看到IOS端做了这个加载数据的效果,为了保持UI一致,就自定义了Loading Dialog ! 


先看一下大概的效果:



下面就是代码部分:

   res/drawable/

        

<?xml version="1.0" encoding="utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android"    android:fromDegrees="0"    android:pivotX="50%"    android:pivotY="50%"    android:toDegrees="360" >    <shape        android:innerRadiusRatio="3"        android:shape="ring"        android:thicknessRatio="20"        android:useLevel="false"        >        <gradient            android:centerColor="@color/colorAccent" // 主题控件颜色,可以根据需要给定            android:centerY="0.50"            android:endColor="@color/colorAccent"            android:startColor="#00ffffff"            android:type="sweep"            android:useLevel="false" />    </shape></rotate>

 Dialog主要布局:

       layout/ 

        

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/ll_dialog"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingTop="15dp"    android:paddingRight="25dp"    android:paddingLeft="25dp"    android:paddingBottom="5dp"    android:gravity="center">       <ProgressBar        android:id="@+id/pb_progress_bar"        android:layout_width="50dp"        android:layout_height="50dp"        android:indeterminateDrawable="@drawable/progressbar"        android:indeterminateDuration="700"        android:visibility="visible"        />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="7dp"        android:text="New Text"        android:id="@+id/tv_loading"        android:layout_gravity="center_horizontal" /></LinearLayout>

   主要代码部分:

        java:

         

/** * * @param context * @param isAlpha 是否需要透明度 * @param message 显示加载的内容 * @return */public static Dialog writeDialog(Context context,boolean isAlpha,String message){    Dialog progressDialog = new Dialog(context);    progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);    if (isAlpha){        WindowManager.LayoutParams lp= progressDialog.getWindow().getAttributes();        lp.alpha=0.8f;        progressDialog.getWindow().setAttributes(lp);    }    LayoutInflater inflater = LayoutInflater.from(context);    View v = inflater.inflate(R.layout.dialog, null);    LinearLayout layout = (LinearLayout) v.findViewById(R.id.ll_dialog);    ProgressBar pb_progress_bar = (ProgressBar) v.findViewById(R.id.pb_progress_bar);    pb_progress_bar.setVisibility(View.VISIBLE);    TextView tv  = (TextView) v.findViewById(R.id.tv_loading);    if (message==null||message.equals("")){        tv.setVisibility(View.GONE);    }else{        tv.setText(message);
        //R.color.colorAccent 我是拿的主题控件颜色,可以根据需要给
  tv.setTextColor(context.getResources().getColor(R.color.colorAccent)); } progressDialog.setContentView(layout, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); return progressDialog;}

Activity:

    java:

    progressDialog = DialogUtil.writeDialog(this,false,"正在加载");    progressDialog.show();    progressDialog.setCanceledOnTouchOutside(false);//设置点击屏幕加载框不会取消(返回键可以取消)

     

            

0 0