众所周知,对话框常用于Android系统中,但是Android自带的对话框有时候不能满足我们的需求,于是我们可以自己自定义对话框来进行我们的开发。
Android系统自带的对话框
Android系统下经过处理过的对话框
下面我们来简述一下步骤
1. 准备图片资源,包括对话框的背景和按钮的背景等等
2. 进行我们需要对话框的自定义布局
3. 重写继承AlertDialog的类
4. 调用我们自定义的对话框
好,下面来详解
1. 准备图片资源,包括对话框的背景和按钮的背景等等
没有什么说的,用PS做即可,很简单,主要是渐变和透明度及羽化效果
2. 布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_exit"
android:orientation="vertical"
android:layout_width="260sp"
android:layout_height="200sp"
android:paddingTop="10sp"
android:paddingLeft="20sp"
android:paddingRight="20sp"
android:gravity="center"
android:background="@drawable/dialog_bg"
>
<TextView android:id="@+id/exit_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/is_exit"
android:textSize="20sp"
android:layout_marginBottom="20sp"
android:textColor="@color/whitesmoke"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="200sp"
android:layout_height="wrap_content"
android:gravity="center"
>
<Button android:id="@+id/bt_exit_ok"
android:layout_width="80sp"
android:layout_height="wrap_content"
android:focusable="true"
android:background="@drawable/xml_button_bg"
android:textColor="@color/whitesmoke"
android:layout_margin="5sp"
android:text="@string/ok"/>
<Button android:id="@+id/bt_exit_cancle"
android:layout_width="80sp"
android:focusable="true"
android:layout_height="wrap_content"
android:background="@drawable/xml_button_bg"
android:textColor="@color/whitesmoke"
android:layout_margin="5sp"
android:text="@string/cancel"/>
</LinearLayout>
</LinearLayout>
对话框的样式 value/style
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="iPone_dialogbg" 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">@color/alpha_00</item><!--背景透明-->
<item name="android:backgroundDimEnabled">false</item><!--模糊-->
</style>
</resources>
3. 重写继承AlertDialog的类
package com.inhand.iphone;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
/**
*
* 仿iPhone的对话框
* @author Handroid
* @version 1.0
* @since 2012-01-17
*/
public class IPhoneDialog extends AlertDialog{
protected IPhoneDialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public IPhoneDialog(Context context, int theme) {
super(context, theme);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_exit);
}
}
4. 在主程序中调用
package com.inhand.iphone;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
/**
*
* 仿iPhone的对话框
* @author Handroid
* @version 1.0
* @since 2012-01-17
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);//无标题栏
setContentView(R.layout.main);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
//仿苹果 对话框
final IPhoneDialog iphone_dialog =
new IPhoneDialog(MainActivity.this, R.style.iPone_dialogbg);//执行构造方法
Window win = iphone_dialog.getWindow();//获得窗口
LayoutParams params = new LayoutParams();
win.setAttributes(params);
iphone_dialog.setCancelable(true);
iphone_dialog.show();
Button bt_ok = (Button)iphone_dialog.findViewById(R.id.bt_exit_ok);
bt_ok.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
iphone_dialog.dismiss();
MainActivity.this.finish();
System.exit(0);
}
});
Button bt_cancel = (Button)iphone_dialog.findViewById(R.id.bt_exit_cancle);
bt_cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
iphone_dialog.dismiss();
}
});
return true;
}
return super.onKeyDown(keyCode, event);
}
}
源代码下载http://download.csdn.net/detail/zhangjianwuxian/4028531