Android 自定义dialog(封装好的)

喜欢把用过的东西总结一下,既可以分享,以后自己用的时候也方便查;废话不多说,先看效果:

 Android 自定义dialog(封装好的)_第1张图片

这是一个很常用的dialog ,封装一个是有必要的,用的地方太多了,实现步骤:

1:创建一个progress_dialog.xml

<?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:background="#ffffffff"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="#7f000000"
        android:orientation="horizontal"
        android:padding="5dp" >

        <ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#00000000"
            android:layout_gravity="center_vertical" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="3dp"
            android:text="亲,正在为您加载!"
            android:textColor="#ffffffff"
            android:textSize="17sp" />
    </LinearLayout>

</LinearLayout>

2:修改styles风格:

   <style name="dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <!-- 边框 -->
        <item name="android:windowIsFloating">true</item>
        <!-- 是否浮现在activity之上 -->
        <item name="android:windowIsTranslucent">false</item>
        <!-- 半透明 -->
        <item name="android:windowNoTitle">true</item>
        <!-- 无标题 -->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!-- 背景透明 -->
        <item name="android:backgroundDimEnabled">false</item>
        <!-- 模糊 -->
    </style>

3:代码类:

package com.dialog.demo;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;

public class CProgressDialog
{
	private Context	mContext;
	private Dialog	mDialog;

	public CProgressDialog(Context context)
	{
		mContext = context;
	}

	public Dialog loadDialog()
	{
		mDialog = new Dialog(mContext, R.style.dialog);
		LayoutInflater in = LayoutInflater.from(mContext);
		View viewDialog = in.inflate(R.layout.progress_dialog, null);
		viewDialog.setBackgroundColor(0x7f000000);
		mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		// 这里可以设置dialog的大小,当然也可以设置dialog title等
		// LayoutParams layoutParams = new LayoutParams(width * 80 / 100, 50);
		// mDialog.setContentView(viewDialog, layoutParams);
		mDialog.setContentView(viewDialog);
		mDialog.setCanceledOnTouchOutside(true);
		mDialog.show();
		return mDialog;
	}

	public void removeDialog()
	{
		mDialog.dismiss();
	}
}

4:代最后引用地方:

package com.dialog.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;

public class MainActivity extends Activity
{
	private CProgressDialog	mDialog;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mDialog = new CProgressDialog(this);
		mDialog.loadDialog();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		mDialog.removeDialog();
		return super.onTouchEvent(event);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

这样就大功告成了

 源码下载


 

你可能感兴趣的:(Android 自定义dialog(封装好的))