2011-12-31 31 views
13

Tôi tạo một mã số AlertDialog bằng mã bên dưới. Vì lý do nào đó tôi nhận được một nền phụ (xem ảnh) trên Honeycomb và ở trên. Mã số treo phạt cho mọi thứ bên dưới tổ ong. MyCustomDialog chỉ đơn giản là Theme.Dialog cho < API-11 và Theme.Holo.Dialog cho API-11 trở lên.Android Alert Dialog Background Issue API 11+

  1. Bất kỳ ý tưởng nào về lý do tôi nhận được nền tảng bổ sung?
  2. Bất kỳ ý tưởng nào tại sao nó bị lỗi cho API < 11? Nó hoạt động tốt nếu tôi loại bỏ các Theme.

Cập nhật tìm câu trả lời cho Câu hỏi # 2. Dường như các nhà xây dựng AlertDialog.Builder(Context context, int theme) đã được giới thiệu trong API 11. sửa chữa của tôi chỉ đơn giản là thay đổi dòng để:

final AlertDialog.Builder builder = (Integer.parseInt(android.os.Build.VERSION.SDK) < 11)? new AlertDialog.Builder(this) : new AlertDialog.Builder(this,R.style.JumpDialog); 

tôi vẫn cần giúp đỡ Câu hỏi # 1

enter image description here

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; 
} 

Trả lời

17

đặn ra câu trả lời

  1. AlertDialog có hằng số tĩnh cho mỗi chủ đề trong lớp AlertDialog và nó không có chủ đề tiêu chuẩn. khi tôi thay thế R.style.MyTheme hoặc android.R.style.Theme_Holo_Dialog bằng AlertDialog.THEME_HOLO_LIGHT mã chỉ hoạt động tiền phạt.
  2. Có vẻ các nhà xây dựng AlertDialog.Builder(Context context, int theme) đã được giới thiệu trong API 11. sửa chữa của tôi chỉ đơn giản là thay đổi dòng tới:

    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); 
    } 
    
+0

Từ 1.6 trở lên, bạn nên sử dụng Build.VERSION.SDK_INT thay vì vì Build.VERSION.SDK không được dùng nữa. – bCliks

+0

đã cập nhật câu trả lời để xóa api không dùng nữa và mức api được mã hóa cứng – Ali

+0

để "Integer.parseInt" không cần thiết nữa –

6

Bạn có thể cố gắng sử dụng new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.JumpDialog)) thay vì new AlertDialog.Builder(this, R.style.JumpDialog)

2

Đối với những tìm kiếm một cách để tùy chỉnh chủ đề của hộp thoại mà không phải gắn bó với chủ đề mặc định (như trong giải pháp được chấp nhận), bắt đầu bằng Lollipop có vẻ như nền cuối cùng đã bị xóa. Vì vậy, bây giờ bạn có thể tạo ra một chủ đề kế thừa từ một mặc định (ví dụ với 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> 

Và nhanh chóng xây dựng của bạn với mã này:

AlertDialog.Builder builder = new AlertDialog.Builder(
      getActivity(),     
      Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT ? 
        R.style.SelectionDialog : 
        R.style.SelectionDialog_PreL); 

Tất nhiên điều này cũng có thể được thực hiện với nguồn tài nguyên thư mục (values/values-v21/).