自定义progressDialog

开发中经常需要用到progressDialog,但自带的太挫了,自定义吧

效果图:

 

1,需要为Dialog定义一个布局文件,我这里定义一个waiting.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:gravity="center">
    
	<TextView 
	    android:id="@+id/waiting_tv"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_alignParentTop="true"
	    android:text="请等待^.^"/>
	
	<ImageView android:id="@+id/waiting_iv"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_below="@id/waiting_tv"
	    />
</RelativeLayout>

 

2,dialog里面有个逐帧动画,定义一个animation-lis放于drawable文件夹

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="false">
    <!-- oneshot false表示循环 -->
    <item android:drawable="@drawable/comment_load1" android:duration="150"/>
	<item android:drawable="@drawable/comment_load2" android:duration="150"/>
	<item android:drawable="@drawable/comment_load3" android:duration="150"/>
	<item android:drawable="@drawable/comment_load4" android:duration="150"/>
</animation-list>


3,自定义的dialog控件

public class WaitingDialog extends Dialog{

	private TextView tv;
	private ImageView iv;
	private AnimationDrawable mAnimationDrawable;
	
	
	public WaitingDialog(Context context, int theme) {
		super(context, theme);
		// TODO Auto-generated constructor stub
	}

	protected WaitingDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		// TODO Auto-generated constructor stub
	}

	public WaitingDialog(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		View view = LayoutInflater.from(getContext()).inflate(R.layout.waiting, null);
		tv = (TextView) view.findViewById(R.id.waiting_tv);
		iv = (ImageView) view.findViewById(R.id.waiting_iv);
		
		iv.setImageResource(R.drawable.waitting);
		
		
		setContentView(view);
	}
	
	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
		mAnimationDrawable = (AnimationDrawable) iv.getDrawable();
		mAnimationDrawable.start();
	}
	
	@Override
	public void dismiss() {
		// TODO Auto-generated method stub
		super.dismiss();
		mAnimationDrawable = (AnimationDrawable) iv.getDrawable();
		mAnimationDrawable.stop();
	}
	
	public void setMsg(String txt) {
		tv.setText(txt);
	}
	
}


4,为dialog设置外观样式,有没有title啊,边框啊,什么的,不会写就参考Themes.xml这个文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--  -->
    <style name="WaitingDialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
    </style>
</resources>


5,在Activity调用,注意一下 传的是 Activity.this,如果传getBaseContext(),或者getApplicationContext()的话会报windowstoken异常

    private void WaitingDialogTest() {
    	Dialog mDialog = new WaitingDialog(this, R.style.WaitingDialog);
    	mDialog.show();
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值