AlertDialog 自定义

一个自定义加载框,资源布局如下

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="10dp"/>
    <solid android:color="#333"/>
</shape>

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="10dp" />
    <solid android:color="#aaa"/>
    <size android:height="10dp"
        android:width="10dp"/>
</shape>

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="10dp" />
    <solid android:color="#fff"/>
    <size android:height="10dp"
        android:width="10dp"/>
</shape>



import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

/**
 * Created by Administrator on 2016/5/18.
 */
public class LoaddingDialog {

    String TAG = "LoaddingDialog";

    private AlertDialog dialog;
    private View[] indictors;
    private int indictorCount = 3;
    private int current = 0;
    private boolean running = false;
    private Drawable hover;
    private Drawable normal;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            indictors[0].setBackgroundDrawable(normal);
            indictors[1].setBackgroundDrawable(normal);
            indictors[2].setBackgroundDrawable(normal);
            indictors[msg.what].setBackgroundDrawable(hover);
        }
    };

    public void show(){
        dialog.show();
        if (running)return;
        new Thread(new LoadThread()).start();
    }

    public void hide(){
        running = false;
        dialog.dismiss();
    }

    public LoaddingDialog(Context context) {
        init(context, true);
    }

    public LoaddingDialog(Context context, boolean cancelable){
        init(context, cancelable);
    }

    private void init(Context context, boolean cancelable){
        View root = LayoutInflater.from(context).inflate(R.layout.layout_loadding, null);
        indictors = new View[indictorCount];
        indictors[0] = root.findViewById(R.id.loadding_indictor1);
        indictors[1] = root.findViewById(R.id.loadding_indictor2);
        indictors[2] = root.findViewById(R.id.loadding_indictor3);

        dialog = new AlertDialog.Builder(context)
                .setCancelable(cancelable)
                .create();
        dialog.setView(root);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.setOnDismissListener(listener);
//        dialog.getWindow().setDimAmount(0);
        hover = context.getResources().getDrawable(R.drawable.shape_loadding_indictor_hover);
        normal = context.getResources().getDrawable(R.drawable.shape_loadding_indictor);
    }

    DialogInterface.OnDismissListener listener = new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            if (running)
                running = false;
        }
    };

    class LoadThread implements Runnable{
        @Override
        public void run() {
            current = 0;
            running = true;
            long time = System.currentTimeMillis();
            Log.e("state", "start----------");
            while(running){
                if (System.currentTimeMillis() - time < 300){
                    continue;
                }
                time = System.currentTimeMillis();
                handler.sendEmptyMessage(current);
                current= ++current%3;
            }
            Log.e("state", "end----------");
        }
    }

}


需要注意的点:

1、能够自定义view的是alertdialog

2、在自定义中需要先create然后再使用getWindow进行样式调整,或者先定义style 直接传入builder 构建函数中

3、使得dialog背景消失,需要使用

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
4、如果要后面背景(dialog未覆盖的其它部分)黑色变掉,使用<pre name="code" class="java">dialog.getWindow().setDimAmount(0);它的值0~1 0是全透明, 父窗口不受影响

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值