![](/img/trans.png)
[英]how to display contacts in a listview in Android for Android api 11+
[英]Android Alert Dialog Background Issue API 11+
我使用下面的代碼創建一個AlertDialog
。 出於某種原因,我在Honeycomb及以上版本上獲得了額外的背景(見圖)。 代碼在蜂窩以下的任何地方崩潰都很好。 MyCustomDialog
只是Theme.Dialog
為<API-11和Theme.Holo.Dialog
為API-11和向上。
更新找到了問題#2的答案。 似乎構造函數AlertDialog.Builder(Context context, int theme)
是在API 11中引入的。我的修復只是將行更改為:
final AlertDialog.Builder builder = (Integer.parseInt(android.os.Build.VERSION.SDK) < 11)? new AlertDialog.Builder(this) : new AlertDialog.Builder(this,R.style.JumpDialog);
我仍然需要問題#1的幫助
private Dialog setupKeyBoardDialog() {
if (mContact.getLocaleId() != -1) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.MyCustomDialog);
builder.setTitle("Keyboards");
mKeyboardLayouts = new KeyboardLayoutGroup();
mKeyboardLayouts.layoutNames = new CharSequence[(int) jni.getNumKeyLayouts()];
mKeyboardLayouts.layoutValue = new ArrayList<Integer>();
for (int i = 0; i < jni.getNumKeyLayouts(); i++) {
mKeyboardLayouts.layoutNames[i] = jni.LayoutInfoForIndex(i).getName();
mKeyboardLayouts.layoutValue.add(i, (int) jni.LayoutInfoForIndex(i).getLocale_id());
}
final int selectedItem = mKeyboardLayouts.layoutValue.indexOf(mContact.getLocaleId());
builder.setSingleChoiceItems(mKeyboardLayouts.layoutNames, selectedItem, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
mContact.setLocaleId(mKeyboardLayouts.layoutValue.get(item));
mContactsDB.saveContact(mContact, true);
dialog.dismiss();
initializeSettingsList();
}
});
final AlertDialog dialog = builder.create();
dialog.setButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogBox, int arg1) {
dialogBox.cancel();
}
});
return dialog;
}
return null;
}
找出答案
R.style.MyTheme
或android.R.style.Theme_Holo_Dialog
與AlertDialog.THEME_HOLO_LIGHT
代碼工作就好了。 似乎構造函數AlertDialog.Builder(Context context, int theme)
是在API 11中引入的。我的修復只是將行更改為:
final AlertDialog.Builder builder; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { builder = new AlertDialog.Builder(this); } else { builder = new AlertDialog.Builder(this,R.style.JumpDialog); }
您可以嘗試使用new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.JumpDialog))
而不是new AlertDialog.Builder(this, R.style.JumpDialog)
對於那些尋找自定義對話框主題的方法而不必堅持默認的方法(如在已接受的解決方案中),從Lollipop開始,似乎最終刪除了額外的背景。 那么,現在您可以創建一個繼承自默認主題的主題(使用AppCompat的示例):
<!-- default theme for L devices -->
<style name="SelectionDialog" parent="Theme.AppCompat.Light.Dialog">
<item name="android:textColor">@color/default_text_color_holo_light</item>
</style>
<!-- theme for Pre-L devices -->
<style name="SelectionDialog.PreL">
<!-- remove the dialog window background -->
<item name="android:windowBackground">@color/transparent</item>
</style>
並使用以下代碼實例化您的構建器:
AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity(),
Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT ?
R.style.SelectionDialog :
R.style.SelectionDialog_PreL);
當然,也可以使用資源文件夾( values/
和values-v21/
)來完成。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.