您的位置:首页 > Web前端 > CSS

AlertDialog 自定义样式

2014-08-02 14:44 489 查看
废话不多说了,只是做个笔记,以后忘了才可以查看下,我一般都不不去记这些方法的,知道了就好,要用的时候查下就可以了。

关键性代码:

/**
	 * 用户名或密码错误的弹窗
	 * 网络状况不好的弹窗
	 */
	public void show_login_error(String err_text){
		final AlertDialog dlg = new AlertDialog.Builder(this).create();
		 dlg.show();
		 Window window = dlg.getWindow();
		// *** 主要就是在这里实现这种效果的.
		// 设置窗口的内容页面,login_error_butt.xml文件中定义view内容
		 window.setContentView(R.layout.pupwindow_login_error);
		 Button login_err = (Button) window.findViewById(R.id.login_error_butt);
		 TextView text=(TextView) window.findViewById(R.id.login_error_text);
		 text.setText(err_text);
		 login_err.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(final View v) {
					dlg.dismiss();
				}
			});
	}


你要显示的布局文件

pupwindow_login_error.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="@drawable/login_popup_bg"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/login_error_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:gravity="center_horizontal"
        android:text="账号或密码错误"
        android:textSize="18sp"
        />
    <View 
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginRight="5dp"
        android:layout_marginLeft="5dp"
        android:background="#d2d2d2"
        />
	<Button 
	    android:id="@+id/login_error_butt"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:gravity="center_horizontal|center_vertical"
	    android:text="确定"
	    android:background="@null"
	    />
</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: