自定义封装好的对话框,不仅好看, 还好用。
里面可以封装一写设置提示语的方法,到时候一个app用一套统一的对话框是很好的。
1、首先继承自dialog
package com.example.myslefdialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* @author gaobingbing
*
*/
public class LoadingDialog extends Dialog {
private View layout;
private ImageView iv_load;
private AnimationDrawable adShaking;
public LoadingDialog(Context context) {
super(context, R.style.TranslucentDialog);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.loading_dialog, null);
setContentView(layout);
// 设置window属性
LayoutParams attributes = getWindow().getAttributes();
attributes.gravity = Gravity.CENTER;
getWindow().setAttributes(attributes);
iv_load=(ImageView)layout.findViewById(R.id.iv_loading_dialog);
adShaking=(AnimationDrawable)iv_load.getDrawable();
adShaking.start();
}
/**
* 加载提示语
*
* @param message
* 提示语
*/
public void setMessage(String message) {
TextView loadingMessage = (TextView) layout
.findViewById(R.id.loading_message);
loadingMessage.setText(message);
}
/**
* 显示生活小提示
*
* @param lifeTipContent
* 生活小提示内容
*/
public void showLifeTips(String lifeTipContent) {
if (lifeTipContent != null && lifeTipContent.length() > 0) {
LinearLayout lifeTipLayout = (LinearLayout) layout
.findViewById(R.id.life_tip_layout);
lifeTipLayout.setVisibility(View.VISIBLE);
TextView tipContent = (TextView) layout
.findViewById(R.id.life_tip_content);
tipContent.setText(lifeTipContent);
}
}
}
2.它的布局文件
loading_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv_loading_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@anim/load_amin"
android:scaleType="fitCenter" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/load_circle" />
<TextView
android:id="@+id/loading_message"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dip"
android:text="拼命加载中..."
android:textColor="#FFFFFF" />
<LinearLayout
android:id="@+id/life_tip_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/life_tip_bg"
android:gravity="center_vertical"
android:visibility="gone" >
<TextView
android:id="@+id/life_tip_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="95dp"
android:layout_marginRight="6dp"
android:textColor="#FFFFFF"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
3.自定义的对话框的背景设置为透明
<style name="TranslucentDialog" parent="android:style/Theme.Dialog">
<item name="android:background">#00000000</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
4.要使用的动画
load_amin.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画
根标签下,通过item标签对动画中的每一个图片进行声明
android:duration 表示展示所用的该图片的时间长度
-->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item
android:drawable="@drawable/load_gril1"
android:duration="100">
</item>
<item
android:drawable="@drawable/load_gril2"
android:duration="100">
</item>
<item
android:drawable="@drawable/load_gril3"
android:duration="100">
</item>
<item
android:drawable="@drawable/load_gril4"
android:duration="100">
</item>
<item
android:drawable="@drawable/load_gril5"
android:duration="100">
</item>
<item
android:drawable="@drawable/load_gril6"
android:duration="100">
</item>
</animation-list>
5.使用
LoadingDialog loadingDialog=new LoadingDialog(this);
loadingDialog.showLifeTips("我好爱你!");
loadingDialog.show();