1. 程式人生 > >Android中使Dialog顯示時背景不變暗

Android中使Dialog顯示時背景不變暗

有兩種方法:都是使用Style指定相關的屬性

1.在style中顯示的設定

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="DialogStyle" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground"> @android:color/transparent </item>
        <item name="android:backgroundDimEnabled">false</item>    </style>    
</resources>


在Activity中覆寫onCreateDialog()方法

@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		if(id == 1){
			
			Dialog dialog = new Dialog(this, R.style.DialogStyle);
			dialog.setContentView(R.layout.start_dialog);
				
			dialog.show();
			isDialogShow = true;
			return dialog;
		}		
		
		return super.onCreateDialog(id);
		
	}


2.在程式碼中使用語句設定

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="DialogStyle" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground"> @android:color/transparent </item>
        
    </style>    
</resources>

java程式碼

@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		if(id == 1){
			
			Dialog dialog = new Dialog(this, R.style.DialogStyle);
			dialog.setContentView(R.layout.start_dialog);
			
			Window window = dialog.getWindow();
			WindowManager.LayoutParams params = window.getAttributes();
			params.dimAmount = 0f;
			window.setAttributes(params);	
			dialog.show();
			isDialogShow = true;
			return dialog;
		}		
		
		return super.onCreateDialog(id);
		
	}

兩種方法其實一樣,最後效果就是彈出對話方塊時背景不會變暗了。