AlertDialog 自定义样式

废话很少说了,只是作个笔记,之后忘了才能够查看下,我通常都不不去记这些方法的,知道了就好,要用的时候查下就能够了。java


关键性代码:android

/**
	 * 用户名或密码错误的弹窗
	 * 网络情况很差的弹窗
	 */
	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.xmlide

<?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>
相关文章
相关标签/搜索