Android自定义AlertDialog组件

最近由于开发的需要想自定义一个AlertDialog对话框,苦于自己太菜网上搜了不少资料也向朋友偷师了不少(好资料自定义多选项对话框)。感觉还是有点迷糊。
虽然迷糊但还是做出来了。过程分享一下。

一,为什么要自定义AlertDialog对话框,想自定义AlertDialog对话框的朋友大多数因为系统的AlertDialog与自己开发的应用不搭调或者在想在AlertDialog增加一下网格布局,
线性布局等比较复杂的页面操作,这是后系统自带的AlertDialog往往难以实现所以需要自定义AlertDialog。

二,如何自定义AlertDialog?
   1,定义Dialog的xml布局文件。一般如果自定义Dialog都自定义标题(titile)信息message和确定,取消按钮。以下是自定义的xml文件

customdialog.xml

<?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:background="@color/base_bg"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@drawable/custom_blue"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="标题"
            android:textColor="@color/white"
            android:textSize="20sp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:minHeight="60dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tv_msg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="15dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="15dp"
            android:textColor="@color/black"
            android:text="标题"
            android:textSize="16sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/bt_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:minWidth="100dp"
            android:text="@string/cancel" />

        <Button
            android:id="@+id/bt_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="15dp"
            android:minWidth="100dp"
            android:text="@string/sure" />
    </LinearLayout>

</LinearLayout>


   2,创建继承Dialog类的CustomDialog类。 在该类构造函授中有一个R.styles.customdialog参数是指定该dialog风格。详细第3说明。

public class CustomDialog extends Dialog implements
		android.view.View.OnClickListener {


	protected Context context;
	protected TextView tv_title;
	protected TextView tv_message;
	protected Button bt_left;
	protected Button bt_right;
	protected ButtonRespond res;//自定义的一个相应按钮点击事件的接口

	public CustomDialog(Context context, ButtonRespond res) {
		super(context, R.style.custom_dialog);
		this.context = context;
		this.res = res;	
	        this.setContentView(R.layout.customdialog);
		tv_title = (TextView) this.findViewById(R.id.tv_title);
		tv_message = (TextView) this.findViewById(R.id.tv_msg);
		bt_left = (Button) this.findViewById(R.id.bt_left);
		bt_left.setOnClickListener(this);
		bt_right = (Button) this.findViewById(R.id.bt_right);
		bt_right.setOnClickListener(this);	
	}
}


  3.在android的stlyle文件中增加一个名为customdialog的style(如果项目还没有styles文件先创建在此不煞述)如下:

       <style name="custom_dialog" parent="@android:style/Theme.Dialog">

        <!-- 边框 -->
        <item name="android:windowFrame">@null</item>
        <!-- 是否浮现在activity之上 -->
        <item name="android:windowIsFloating">true</item>
        <!-- 屏幕背景变暗 -->
        <item name="android:backgroundDimEnabled">true</item>
        <!-- 半透明 -->
        <item name="android:windowIsTranslucent">false</item>
        <!-- 无标题 -->
        <item name="android:windowNoTitle">true</item>
        <!-- 背景透明 -->
        <item name="android:windowBackground">@android:color/transparent</item>
     </stytle>

因为自定义了title的缘故所以应该设置为NoTitle形式
4.设置窗体属性:大小,位置即一下一段代码(这段代码本人比较模糊不做解释),如果不增加一下代码则系统用默认的大小(根据文件需要代销WrapContent)居中。该段代码可以放在重写的protected void onCreate(Bundle savedInstanceState)方法里面也可以放在构造函数里面


 

    this.setContentView(R.layout.customdialog);
        Window window = getWindow();

        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;//宽度
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;//高度
        // Put window on top of input method
        lp.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
        window.setAttributes(lp)


  5,其他跟定义一个Activity一样findview和onclicklistener;

6 自定义 CustomDialog.java示例

package com.yuantel.meeting.view;

import com.yuantel.meeting.R;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.TextView;

public class CustomDialog extends Dialog implements
		android.view.View.OnClickListener {


	protected Context context;
	protected TextView tv_title;
	protected TextView tv_message;
	protected Button bt_left;
	protected Button bt_right;
	protected ButtonRespond res;

	public CustomDialog(Context context, ButtonRespond res) {
		super(context, R.style.custom_dialog);
		this.context = context;
		this.res = res;
		// TODO Auto-generated constructor stub
		this.setContentView(R.layout.customdialog);
		tv_title = (TextView) this.findViewById(R.id.tv_title);
		tv_message = (TextView) this.findViewById(R.id.tv_msg);
		bt_left = (Button) this.findViewById(R.id.bt_left);
		bt_left.setOnClickListener(this);
		bt_right = (Button) this.findViewById(R.id.bt_right);
		bt_right.setOnClickListener(this);

//		WindowManager m = (WindowManager) context
//				.getSystemService(Context.WINDOW_SERVICE);
//
//		Display d = m.getDefaultDisplay();
//		LayoutParams p = getWindow().getAttributes();
//		p.width = (int) (d.getWidth() * 0.85);
//
//		getWindow().setAttributes(p);
//		getWindow().setGravity(Gravity.CENTER);
		
		
	}

	@Override
	public void setTitle(CharSequence title) {
		// TODO Auto-generated method stub
		this.tv_title.setText(title);
	}

	@Override
	public void setTitle(int titleId) {
		// TODO Auto-generated method stub
		this.tv_title.setText(titleId);
	}

	/**
	 * 设置对话提示信息
	 * 
	 * @param message
	 *            提示信息
	 */
	public void setMessage(CharSequence message) {
		this.tv_message.setText(message);
	}
	/**
	 * 设置对话提示信息
	 * 
	 * @param messageId
	 *            提示信息id
	 */
	public void setMessage(int messageId) {
		this.tv_message.setText(messageId);
	}

	/**
	 * 设置左边按钮的文字
	 * @param text
	 */
	public void setLeftButtonText(CharSequence text) {

		bt_left.setText(text);
	}

	/**
	 * 	
	 * 设置左边按钮的文字
	 * @param textId
	 */
	public void setCancleButtonText(int textId) {
		bt_left.setText(textId);
	}

	/**
	 * 设置右边按钮的文字
	 * @param text
	 */
	public void setRightButtonText(CharSequence text) {

		bt_right.setText(text);
	}

	/**
	 * 	
	 * 设置右边按钮的文字
	 * @param textId
	 */
	public void setRightButtonText(int textId) {
		bt_right.setText(textId);
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.bt_left:
			res.respondLeftButton();
			break;
		case R.id.bt_right:
			res.respondRightButton();
			break;

		default:
			break;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		WindowManager m = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);

		Display d = m.getDefaultDisplay();
		LayoutParams p = getWindow().getAttributes();
		p.width = (int) (d.getWidth() * 0.85);

		getWindow().setAttributes(p);
		getWindow().setGravity(Gravity.CENTER);
	}
}

接口类ButtonRespond.java

package com.yuantel.meeting.view;

public interface ButtonRespond {

	public void respondLeftButton();

	public void respondRightButton();

}


效果图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值