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>
相關文章
相關標籤/搜索