好久沒寫博文了,今天我跟大家介紹一種dialog ,相信對大家的幫助一定很多.當我們提交一個請求道服務器,在沒有得到請求前不準用戶進行任何操作,屏幕出現一個這樣的dialog
當得到網網速結果時dialog.dismiss()消失,用戶重新可以操作界面了. 這樣的一個dialog是不是大大的簡便了我們隊應用程序的控制呢.好了廢話少說進入正題:
1,首先你必須自定義layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_view"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minHeight="60dp"
android:minWidth="180dp"
android:gravity="center"
android:padding="10dp"
>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/publicloading"
/>
<TextView
android:id="@+id/tipTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp" />
</LinearLayout>
關鍵是imageView 不能少因爲旋轉的效果是通過imageview調用startAnimation()方法實現的,下面的textview是提示用的.
2.自定義一個style
<style name="loading_dialog" parent="android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@drawable/transpant_bg</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
記住transpant_bg是透明的transpant_bg.xml內容如下
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="@android:color/transparent"
android:endColor="@android:color/transparent"
android:angle="270.0" >
</gradient>
</shape>
3,最後是代碼實現了
public static Dialog createLoadingDialog(Context context, String msg) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.toast, null);// 得到加載view
LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加載佈局
// main.xml中的ImageView
ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);
TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字
// 加載動畫
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(
context, R.anim.loading_animation);
// 使用ImageView顯示動畫
spaceshipImage.startAnimation(hyperspaceJumpAnimation);
tipTextView.setText(msg);// 設置加載信息
Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);// 創建自定義樣式dialog
loadingDialog.setCancelable(false);// 不可以用“返回鍵”取消
loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));// 設置佈局
return loadingDialog;
}
接下來實例用法:當你點擊測試網速是show(), 當接到stop命令式dismiss().夠不夠詳細呢.覺得滿意的話留個言咯,樓主需要鼓勵的.@Override
public void onClick(View v) {
if (mBack.isMyChild(v)) {
Util.finish(this);
} else if (v == mMeasureSpeed) {
mMeasureSpeed.setEnabled(false);
// mBar.setVisibility(View.VISIBLE);
mDialog = Util.createLoadingDialog(this, "");
mDialog.show();
}
private Handler mProgressHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case LOADING:
mBar.setProgress(mProgressState);
break;
case STOP:
mBar.setVisibility(View.GONE);
mDialog.dismiss();
setSpeed();
mMeasureSpeed.setEnabled(true);
break;
default:
break;
}
}
};