前言:因为在项目中须要用到更新显示动画的需求,所以想到了dialog,自己定义dialog不难。网上教程非常多,可是在实现dialog背景透明的需求时,遇到了一点问题。网上的一些方法在我的机器上并没有实现,仅仅能曲折中找到了还有一个方法实现。尽管有点麻烦。但毕竟效果不错。
此方法写在这里,一是和各位分享,二是做个记录,留待以后需求。
不说了,上代码:
以下是dialog自己定义布局文件,是运行任务用的,參考就可以。
xml version="1.0" encoding="utf-8"?
> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="运行中" android:textSize="18sp" /> <ImageView android:id="@+id/dialog_image" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center" /> </LinearLayout>
自己定义dialog,运行任务的代码去掉了,能够加在onCreate()中。
/** * 运行状态的对话框形式 * @author Administrator * */class MyDialog extends Dialog{ ImageView imageView; public MyDialog(Context context) { super(context); // TODO Auto-generated constructor stub } public MyDialog(Context context,int theme) { super(context, theme); // TODO Auto-generated constructor stub } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); //去掉activity标题 requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.dialog); //设置标题 imageView = (ImageView)findViewById(R.id.dialog_image); }}在实现之前。实验了dialog设置flag的方法,有人说能够,可是我并没有实现。也把代码留下,以作參考:
//使dialog失去焦点 dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE); dialog.setCanceledOnTouchOutside(true);真正实现的方法是使用style实现的,这个须要两个资源文件,一个color,一个style。
设置好资源文件之后。在调用时生成实例代码例如以下:#00000000
MyDialog dialog = new MyDialog(this,R.style.dialog); dialog.show();至此完成。
參考资料:http://www.cnblogs.com/windlivefamily/articles/2133956.html