2014-04-11 37 views

Trả lời

33

Cách duy nhất để thay đổi màu sắc chia Dialog tiêu đề là bởi subclassing Dialog và sử dụng Resources.getIdentifier để tìm tiêu đề chia View. Sau đó tất cả những gì bạn cần là một cuộc gọi đến View.setBackgroundColor. Vì đây là cách duy nhất để tùy chỉnh bộ chia tiêu đề, bạn cũng có thể tiếp tục và sử dụng cùng một phương pháp để tùy chỉnh màu tiêu đề.

Nhưng theo như lý do tại sao bạn không thể nhận được câu trả lời bạn liên kết để làm việc cho bạn, thật khó để nói. Bạn không bao gồm bất kỳ mã nào hoặc bất kỳ thứ gì bạn đã thử, do đó, việc này giúp bạn xác định lý do tại sao bạn không nhận được kết quả mong muốn.

Dưới đây là một ví dụ về việc thay đổi màu sắc tiêu đề và màu sắc tiêu đề dải phân cách:

/** 
* A sublcass of {@link AlertDialog} used to customize the title and title 
* divider colors 
*/ 
public class CustomDialog extends AlertDialog { 

    /** 
    * Constructor for <code>CustomDialog</code> 
    * 
    * @param context The {@link Context} to use 
    */ 
    public CustomDialog(Context context) { 
     super(context); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     final Resources res = getContext().getResources(); 
     final int yellow = res.getColor(android.R.color.holo_orange_light); 

     // Title 
     final int titleId = res.getIdentifier("alertTitle", "id", "android"); 
     final View title = findViewById(titleId); 
     if (title != null) { 
      ((TextView) title).setTextColor(yellow); 
     } 

     // Title divider 
     final int titleDividerId = res.getIdentifier("titleDivider", "id", "android"); 
     final View titleDivider = findViewById(titleDividerId); 
     if (titleDivider != null) { 
      titleDivider.setBackgroundColor(yellow); 
     } 
    } 

} 

Thực hiện

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    final CustomDialog customDialog = new CustomDialog(this); 
    customDialog.setTitle("Title"); 
    customDialog.setMessage("Message"); 
    customDialog.show(); 
} 

Sử dụng một DialogFragment với AlertDialog.Builder

public class CustomDialogFragment extends DialogFragment { 

    /** 
    * Empty constructor as per the {@link Fragment} docs 
    */ 
    public CustomDialogFragment() { 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     return new AlertDialog.Builder(getActivity()) 
       .setTitle("Title") 
       .setMessage("Message") 
       .create(); 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 
     final Resources res = getResources(); 
     final int yellow = res.getColor(android.R.color.holo_orange_light); 

     // Title 
     final int titleId = res.getIdentifier("alertTitle", "id", "android"); 
     final View title = getDialog().findViewById(titleId); 
     if (title != null) { 
      ((TextView) title).setTextColor(yellow); 
     } 

     // Title divider 
     final int titleDividerId = res.getIdentifier("titleDivider", "id", "android"); 
     final View titleDivider = getDialog().findViewById(titleDividerId); 
     if (titleDivider != null) { 
      titleDivider.setBackgroundColor(yellow); 
     } 
    } 

} 

Thực hiện

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    new CustomDialogFragment().show(getFragmentManager(), "customDialogFragment"); 
} 

Kết quả

Example

+0

cảm ơn. Nó hoạt động, nhưng có cách nào để tạo ra điều này với AlertDialog.Builder? Tôi đang sử dụng DialogFragments cho các hộp thoại. – Vanama

+0

@Vanama Tôi đã chỉnh sửa bằng một ví dụ sử dụng 'DialogFragment' và' AlertDialog.Builder'. – adneal

+4

Cũng giống như người đứng đầu cho người dùng trong tương lai có thể gặp phải điều này; vì một lý do nào đó, khi tôi sử dụng một Dialog chung tôi phải sử dụng "title" làm tên định danh của tôi thay vì "alertTitle". – zgc7009

Các vấn đề liên quan