android自定义ProgressDialog

来源:互联网 发布:投诉淘宝店家有用吗 编辑:程序博客网 时间:2024/06/15 11:13

本文主要介绍自动环形的ProgressDialog,主要用于网络请求之前的缓冲页面。

首先对ProgressDialog进行分解,它分为一个ProgressBar和一个TextView,因此我们最主要的就是自定义ProgressBar

自定义ProgressDialog的主要布局文件buffer_dialog.xml,主要的作用是让dialog在屏幕的中间位置显示

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    >        <ProgressBar            android:id="@+id/bufferdialog_progressbar"            style="@style/StyleBufferDialoglarge"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:indeterminateDuration="700"            android:layout_centerInParent="true"           />        <TextView android:id="@+id/bufferdialog_message"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_below="@id/bufferdialog_progressbar"            android:layout_marginTop="25dp"            android:layout_centerHorizontal="true"            android:textColor="@color/white"            android:textSize="20sp"            /></RelativeLayout>


自定以ProgressBar主要是自定义它的Stryle,如下

<style name="StyleBufferDialoglarge">        <item name="android:indeterminateDrawable">@drawable/drawable_list_dialog</item>        <item name="android:minWidth">76dip</item>        <item name="android:maxWidth">76dip</item>        <item name="android:minHeight">76dip</item>        <item name="android:maxHeight">76dip</item>        <item name="android:windowBackground">@android:color/white</item>    </style>

通过动画配置文件来绘制进度条与它的动画效果,如下

<?xml version="1.0" encoding="utf-8"?><animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"    android:drawable="@drawable/loading_animation_dialog"    android:pivotX="50.0%"    android:pivotY="50.0%"     android:fromDegrees="0"    android:toDegrees="360"    />

loading_animation_dialog.png就是随意一张圆形图片即可


到这里所有的配置文件已经配置好了,最后将它们装置到代码中

public class BufferDialog extends AlertDialog {private Context mContext;private TextView mMessageView;public BufferDialog(Context context) {super(context);this.mContext = context;}public BufferDialog(Context context, int theme) {super(context, theme);this.mContext = context;}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);LayoutInflater inflater = LayoutInflater.from(mContext);View view = inflater.inflate(R.layout.buffer_dialog, null);mMessageView = (TextView) view.findViewById(R.id.bufferdialog_message);if (message != null)mMessageView.setText(message);setContentView(view);}private CharSequence message;@Overridepublic void setMessage(CharSequence message) {this.message = message;}}

如此就简单实现了自定义ProgressDialog.

0 0
原创粉丝点击