您的位置:首页 > 其它

AlertDialog 自定义

2016-05-19 09:26 417 查看
一个自定义加载框,资源布局如下

<?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是全透明, 父窗口不受影响


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: