首先附上效果图
1、自定义dialog背景
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 边角的圆弧半径 -->
<corners android:radius="10dp" />
<!-- 实心填充 -->
<solid android:color="#ff000000" />
<!-- 描边:一般大小都是1dp -->
<stroke
android:width="1dp"
android:color="#ff303030" />
</shape>
2、自定义dialog布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/dialog_bg"
android:paddingBottom="20dp"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:paddingTop="20dp" >
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
<TextView
android:id="@+id/tv_dialogmsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="加载中..."
android:textColor="#ffa0a0a0"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
3、重写Dialog
package com.view;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.TextView;
import com.example.tzq.R;
public class MyLoadDialog extends Dialog {
private Context context;
private String text;
public MyLoadDialog(Context context, String text) {
super(context);
// TODO Auto-generated constructor stub
this.context = context;
this.text = text;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.dialog_load, null);
setContentView(view, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// getWindow().setBackgroundDrawable(new BitmapDrawable());
getWindow().setBackgroundDrawableResource(android.R.color.transparent); // 去掉边角
if(text != null) {
TextView tv = (TextView) findViewById(R.id.tv_dialogmsg);
tv.setText(text);
}
}
@Override
public void show() {
// TODO Auto-generated method stub
requestWindowFeature(Window.FEATURE_NO_TITLE); // 去掉标题<pre name="code" class="java">
super.show();}}
4、在Activity中调用
显示Dialog
MyLoadDialog loadDialog = new MyLoadDialog(LoginActivity.this, "登录中,请稍后...");
loadDialog.show();
取消Dialog
loadDialog.dismiss();