android 自定义progressDialog
来源:互联网 发布:单片机招聘 编辑:程序博客网 时间:2024/05/29 03:02
首先附上效果图
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" />
-
-
- <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);
-
- this.context = context;
- this.text = text;
- }
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
-
- 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().setBackgroundDrawableResource(android.R.color.transparent);
-
- if(text != null) {
- TextView tv = (TextView) findViewById(R.id.tv_dialogmsg);
- tv.setText(text);
- }
- }
- @Override
- public void show() {
-
- requestWindowFeature(Window.FEATURE_NO_TITLE);
super.show();}}4、在Activity中调用显示Dialog
- MyLoadDialog loadDialog = new MyLoadDialog(LoginActivity.this, "登录中,请稍后...");
- loadDialog.show();
取消Dialog