您的位置:首页 > 移动开发 > Android开发

android loading界面 & 自定义ProgressDialog

2013-08-19 12:44 357 查看
最近项目中 列表页加载数据的时候 需要显示一个加载进度的界面,系统提供的进度条都比较难看,这里需要我们自定义ProgressDialog。

先看效果图:



界面布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<Button
android:id="@+id/bt_open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/custom_progressdialog" />

<include layout="@layout/selfdef_progress_dialog_layout" />

</RelativeLayout>


loading 界面 selfdef_progress_dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/loading"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >

<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateDrawable="@drawable/selfdef_progress_drawable" />

<TextView
android:id="@+id/progress_dialog_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加载中..."
android:textSize="15sp"/>

</LinearLayout>


可以根据业务需要,在此基础上实现更复杂的loading界面。

自定义旋转图片资源 selfdef_progress_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:pivotX="50%"
android:pivotY="50%"
android:drawable="@drawable/progressloading">
</rotate>


MainActivity.java

package com.example.customprogressdialog;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
private LayoutInflater inflater;
private Handler handler = new Handler();
private ProgressDialog mProgressDialog;
private LinearLayout loading;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

inflater = LayoutInflater.from(getApplicationContext());
findView();
}

private void findView() {

Button bt_open = (Button) findViewById(R.id.bt_open);

//应用程序加载界面
loading = (LinearLayout) findViewById(R.id.loading);

handler.postDelayed(new Runnable() {

@Override
public void run() {

loading.setVisibility(View.GONE);

}
}, 3000);

bt_open.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_open:

mProgressDialog = createProgressDialog(this, "加载中...");

mProgressDialog.show();

Thread thread = new Thread(){
public void run() {

handler.postDelayed(new Runnable() {

@Override
public void run() {

if(mProgressDialog!=null && mProgressDialog.isShowing()){
mProgressDialog.dismiss();
}
}
}, 3000);
};
};

thread.start();

break;

default:
break;
}
}

public ProgressDialog createProgressDialog(Context context, String msg) {

View v = inflater
.inflate(R.layout.selfdef_progress_dialog_layout, null);
TextView txt = (TextView) v.findViewById(R.id.progress_dialog_tv);
ProgressDialog dialog = new ProgressDialog(context);
dialog.show();
dialog.setContentView(v);
if (msg != null && !msg.equals(""))
txt.setText(msg);

return dialog;
}

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