最重要的是這兩行代碼
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//設置Dialog沒有標題,需在setContentView之前設置
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);//設置Dialog背景透明效果
MainActivity
public class MainActivity extends ListActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] array = { "不指定Dialog的主題,指定Activity使用系統定義的主題", //
"指定Dialog使用系統定義的主題", //
"設置Dialog無標題、背景透明效果", //
"設置AlertDialog背景透明效果", };
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>(Arrays.asList(array))));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
switch (position) {
case 0:
startActivity(new Intent(this, Activity1.class));
break;
case 1:
startActivity(new Intent(this, Activity2.class));
break;
case 2:
Dialog dialog = new Dialog(this);//直接設置對話框主題
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//設置Dialog沒有標題。需在setContentView之前設置,在之后設置會報錯
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);//設置Dialog背景透明效果
dialog.setContentView(R.layout.layout_dialog);
dialog.show();
break;
case 3:
AlertDialog alertDialog = new AlertDialog.Builder(this).setView(R.layout.layout_dialog).create();
alertDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);//設置Dialog背景透明效果
alertDialog.show();
break;
}
}
}
Activity1
public class Activity1 extends ListActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] array = { "【Theme_Light】AlertDialog 寬高均會壓縮到最小,背景上下部分有多余的黑色",//【不能用】
"Dialog 對於內容而言,寬高均為設置的值,自帶一個標題欄,寬度不會拉伸及填充", //【去掉標題后問題基本不存在了】
//
"【Theme_Holo_Light】AlertDialog 高會壓縮到最小,寬為固定值,背景無多余的黑色", //【能用,但background圓角大於1dp的話,背景會有點小瑕疵】
"Dialog 對於內容而言,寬高均為設置的值,自帶一個標題欄,寬度會被拉伸為一個固定值", //【去掉標題后問題基本不存在了】
//
"【 Theme_DeviceDefault_Light 】\n 設備默認根主題,與手機系統有關", "", //
//
"【Theme_Material_Light】AlertDialog 高會壓縮到最小,寬為固定值,背景無多余的黑色", //【能用,但background圓角大於1dp的話,背景會有點小瑕疵】
"Dialog 對於內容而言,寬高均為設置的值,自帶一個標題欄,寬度會被拉伸為一個固定值", };//【去掉標題后問題基本不存在了】
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>(Arrays.asList(array))));
setTitle("指定Activity使用系統定義的主題");
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
switch (position) {
case 0://
case 1:
SystemThemeActivity.launch(this, android.R.style.Theme_Light, "Theme_Light", position % 2);
break;
case 2://
case 3:
SystemThemeActivity.launch(this, android.R.style.Theme_Holo_Light, "Theme_Holo_Light", position % 2);
break;
case 4://
case 5:
SystemThemeActivity.launch(this, android.R.style.Theme_DeviceDefault_Light, "Theme_DeviceDefault_Light", position % 2);
break;
case 6://
case 7:
SystemThemeActivity.launch(this, android.R.style.Theme_Material_Light, "Theme_Material_Light", position % 2);
break;
}
}
}
Activity2
public class Activity2 extends ListActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] array = { "【AlertDialog.THEME_HOLO_LIGHT】\nAlertDialog 和在Activity中設置此樣式時一樣",//【能用】,但background圓角大於1dp的話,背景會有點小瑕疵
"Dialog 效果真奇葩", //【不能用】
"【AlertDialog.THEME_DEVICE_DEFAULT_】\nAlertDialog 和在Activity中設置此樣式時一樣", //【能用】,但background圓角大於1dp的話,背景會有點小瑕疵
"Dialog 效果真奇葩", //【不能用】
"【Theme_Dialog】\nAlertDialog 效果真奇葩", //【不能用】
"Dialog 效果和在Activity中設置Theme_Light一樣", // 對於內容而言,寬高均為設置的值,自帶一個標題欄,寬度不會拉伸及填充【去掉標題后問題基本不存在了】
"【更改Theme_Dialog部分屬性】AlertDialog 效果和在Activity中設置Theme_Light一樣", //【不能用】寬高均會壓縮到最小,背景上下部分有多余的黑色
"Dialog 非常完美,大部分自定義Dialog都是這么干的!但是高版本推薦使用DialogFragment", };//【非常完美】
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>(Arrays.asList(array))));
setTitle("指定Dialog使用系統定義的主題");
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
switch (position) {
case 0://
case 1:
SystemThemeDialogActivity.launch(this, AlertDialog.THEME_HOLO_LIGHT, "AlertDialog.THEME_HOLO_LIGHT", position % 2);
break;
case 2://
case 3:
SystemThemeDialogActivity.launch(this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT, "AlertDialog.THEME_DEVICE_DEFAULT_LIGHT", position % 2);
break;
case 4://
case 5:
SystemThemeDialogActivity.launch(this, android.R.style.Theme_Dialog,
"Theme_Dialog
", position % 2);
break;
case 6://
case 7:
SystemThemeDialogActivity.launch(this, R.style.DialogTheme, "更改Theme_Dialog部分屬性", position % 2);
break;
}
}
}
SystemThemeActivity
/**
* 不指定Dialog的主題,指定Activity使用系統定義的主題
* @author 白乾濤
*/
public class SystemThemeActivity extends Activity implements OnDismissListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
int themeId = getIntent().getIntExtra("themeId", 0);
String theme = getIntent().getStringExtra("theme");
int type = getIntent().getIntExtra("type", 0);
setTheme(themeId);//設定【Activity的主題】要放到調用父類方法之前
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setTextColor(0xffff0000);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
textView.setText(theme);
setContentView(textView);
//未指定【Dialog的主題】時,Dialog默認使用的是系統提供的dialogTheme主題
if (type == 0) {
textView.append(" AlertDialog");
new AlertDialog.Builder(this).setView(R.layout.layout_dialog).setOnDismissListener(this).create().show();
} else {
textView.append(" Dialog");
Dialog dialog = new Dialog(this);
//dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//設置Dialog沒有標題。需在setContentView之前設置,在之后設置會報錯
dialog.setContentView(R.layout.layout_dialog);
dialog.setOnDismissListener(this);
dialog.show();
}
}
public static void launch(Context context, int themeId, String theme, int type) {
Intent intent = new Intent(context, SystemThemeActivity.class);
intent.putExtra("themeId", themeId);
intent.putExtra("theme", theme);
intent.putExtra("type", type);
context.startActivity(intent);
}
@Override
public void onDismiss(DialogInterface dialog) {
finish();
}
}
SystemThemeDialogActivity
/**
* 指定Dialog使用系統定義的主題
* @author 白乾濤
*/
public class SystemThemeDialogActivity extends Activity implements OnDismissListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int themeId = getIntent().getIntExtra("themeId", 0);
String theme = getIntent().getStringExtra("theme");
int type = getIntent().getIntExtra("type", 0);
TextView textView = new TextView(this);
textView.setTextColor(0xffff0000);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
textView.setText(theme);
setContentView(textView);
//未指定【Dialog的主題】時,Dialog默認使用的是系統提供的dialogTheme主題
if (type == 0) {
new AlertDialog.Builder(this, themeId).setView(R.layout.layout_dialog).setOnDismissListener(this).create().show();
} else {
Dialog dialog = new Dialog(this, themeId);//直接設置對話框主題
//dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//設置Dialog沒有標題。需在setContentView之前設置,在之后設置會報錯
dialog.setContentView(R.layout.layout_dialog);
dialog.setOnDismissListener(this);
dialog.show();
}
}
public static void launch(Context context, int themeId, String theme, int type) {
Intent intent = new Intent(context, SystemThemeDialogActivity.class);
intent.putExtra("themeId", themeId);
intent.putExtra("theme", theme);
intent.putExtra("type", type);
context.startActivity(intent);
}
@Override
public void onDismiss(DialogInterface dialog) {
finish();
}
}
樣式
<resources>
<style name="DialogTheme" parent="@android:style/Theme.Dialog">
<!-- 是否不顯示title,這個是最重要的 -->
<item name="android:windowNoTitle">true</item>
<!-- 設置dialog顯示區域外部的背景(透明),注意這里指的是dialog根布局的背景,因為本例中dialog的ContentView有圓角,所以圓角外部區域顯示這個顏色 -->
<item name="android:windowBackground">@android:color/holo_red_light</item>
<!-- 設置dialog的背景(透明),注意這里不僅包含dialog根布局的背景,還包含本例中ImageView圓角外部的背景。此顏色值會覆蓋掉windowBackground的值 -->
<item name="android:background">@android:color/transparent</item>
<!-- 設置灰度的值,當為1時,界面除了我們的dialog內容是高亮顯示之外,其余區域都是黑色的,完全看不到其他內容,系統的默認值是0.5 -->
<item name="android:backgroundDimAmount">0.5</item>
<!-- 是否允許背景灰暗,即是否讓顯示區域以外使用上面設置的黑色半透明背景,設為false時,:backgroundDimAmount的值等價於0 -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 是否有遮蓋 -->
<item name="android:windowContentOverlay">@null</item>
<!-- 設置Dialog的windowFrame框(無) -->
<item name="android:windowFrame">@null</item>
<!-- 是否浮現在activity之上,必須設為true,否則自己獨立占一個界面,這根本就不像是一個對話框了 -->
<item name="android:windowIsFloating">true</item>
<!-- 是否半透明,貌似沒什么卵用 -->
<item name="android:windowIsTranslucent">true</item>
</style>
</resources>
背景
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item><shape>
<solid android:color="#0f0" />
<stroke android:width="1dp" android:color="#00f" />
<corners android:radius="10dp" />
</shape></item>
</selector>
內容
<?xml version="1.0" encoding="utf-8"?>
<!-- 注意:不同主題下顯示效果大不相同 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="180dp"
android:layout_height="180dp"
android:background="@drawable/dialog_bg"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<EditText
android:layout_margin="5dp"
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
附件列表