首页 > 代码库 > Android 仿 腾讯应用宝 漂亮 弹出对话框

Android 仿 腾讯应用宝 漂亮 弹出对话框

技术分享
public class DialogMenu {
	private Context mContext;
	private TextView tv_title, tv_context;
	private Button yes, no;
	private View view;
	private LayoutInflater layoutInflater;
	private Dialog loadingDialog;

	public DialogMenu(Context context) {
		this.mContext = context;
		this.layoutInflater = LayoutInflater.from(context);
		onCreateView();
	}

	public void onCreateView() {
		view = layoutInflater.inflate(R.layout.dialog_popup, null);
		tv_title = (TextView) view.findViewById(R.id.textView1);
		tv_context = (TextView) view.findViewById(R.id.textView2);
		yes = (Button) view.findViewById(R.id.button2);
		no = (Button) view.findViewById(R.id.button1);
		loadingDialog = new Dialog(mContext, R.style.loading_dialog);// 创建自定义样式

	}

	public void showDialog() {
		loadingDialog.show();
	}

	public void initUI(String title, String msg, String tv_yes, String tv_no) {

		tv_title.setText(title);
		tv_context.setText(msg);
		no.setText(tv_no);
		yes.setText(tv_yes);
		if (onClickListener != null
				&& onClickListener instanceof OnClickListener) {
			yes.setOnClickListener(onClickListener);
			no.setOnClickListener(onClickListener);
		}
		loadingDialog.setContentView(view);
	}

	private OnClickListener onClickListener;

	public void setOnClickListener(OnClickListener onClickListener) {
		this.onClickListener = onClickListener;
	}
}
</pre><pre name="code" class="java">布局:
<pre name="code" class="java"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"

>

    <RelativeLayout
        android:layout_width="280dp"
        android:layout_height="200dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/xi" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/imageView1"
            android:layout_alignLeft="@+id/imageView1"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="16dp"
            android:gravity="center_vertical"
             android:textColor="#424242"
             android:textSize="15sp"
            android:text="退出提示" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:focusable="false"
            android:text="1个任务正在下载,是否退出?" />
       

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="1dip"
            android:layout_alignParentTop="true"
            android:layout_alignRight="@+id/ly_btn"
            android:layout_marginTop="43dp"
            android:background="@drawable/list_divider" />

        <LinearLayout
            android:id="@+id/ly_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/button1"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:background="@drawable/dialog_btn_cancel"
                android:text="取消"
                android:layout_weight="1"
                android:textColor="#424242"
                android:textSize="15sp" />

            <Button
                android:id="@+id/button2"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:background="@drawable/dialog_btn_ok"
                android:text="确定"
                 android:layout_weight="1"
                android:textColor="#00b0ff"
                android:textSize="15sp" />
        </LinearLayout>

    </RelativeLayout>

</RelativeLayout>


对话框风格:

    <!-- 自定义loading dialog -->
    <style name="loading_dialog" parent="android:style/Theme.Dialog">
		<item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item> 
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
	</style>


Android 仿 腾讯应用宝 漂亮 弹出对话框