本文实例为大家分享了Android实现加载对话框的具体代码,供大家参考,具体内容如下
这里简单说一下两种实现加载对话框的方式:1.使用动画让一个图片旋转 2.使用progressbar。
感觉简单来说,dialog就是一个弹出的window,把自己定义的布局放置到window里面就可以了,加载对话框就是有个加载的动画,核心的地方就是实现这个动画,所所以方法 可以有,对图片添加动画,或者使用progressbar。
第一种方式:使用动画让一个图片旋转
先看一下布局:
<&#63;xml version="1.0" encoding="utf-8"&#63;>
然后自定义Alertdialog,并对图片添加旋转动画:
public class LoadingDialog extends AlertDialog { private final String DEFAULT_TEXT="正在加载"; private ImageView mImageview; private TextView mTextView; private LinearLayout mLayout; private String mText; protected LoadingDialog(Context context) { super(context); // TODO Auto-generated constructor stub } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); mLayout=(LinearLayout) LinearLayout.inflate(getContext(), R.layout.loading_dialog, null); mImageview=(ImageView) mLayout.findViewById(R.id.loading_dialog_pic); mTextView=(TextView) mLayout.findViewById(R.id.loading_dialog_text); loadanimation(); getWindow().setContentView(mLayout); } private void loadanimation() {//对图片添加旋转动画 // TODO Auto-generated method stub Animation anim=AnimationUtils.loadAnimation(getContext(), R.anim.loading_dialog_anim); LinearInterpolator lin = new LinearInterpolator(); anim.setInterpolator(lin); mImageview.setAnimation(anim); } }
看一下xml的动画:
<&#63;xml version="1.0" encoding="utf-8"&#63;>
第二种方式:使用progressbar
首先是一个animation-list:
<&#63;xml version="1.0" encoding="utf-8"&#63;>
看一下布局的实现:
<&#63;xml version="1.0" encoding="utf-8"&#63;>
然后自定义一个alertdialog:
public class LoadingDialog extends AlertDialog { private final String DEFAULT_TEXT="正在加载"; private TextView mTextView; private LinearLayout mLayout; private String mText; protected LoadingDialog(Context context) { super(context); // TODO Auto-generated constructor stub } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); mLayout=(LinearLayout) LinearLayout.inflate(getContext(), R.layout.loading_dialog, null); mTextView=(TextView) mLayout.findViewById(R.id.loading_dialog_text); WindowManager m=(WindowManager) getContext().getSystemService(getContext().WINDOW_SERVICE); int windowwith=m.getDefaultDisplay().getWidth(); int w=windowwith*3/5; int h=300; getWindow().setLayout(w, h);//设置对话框窗体大小 getWindow().setContentView(mLayout); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。