2010-10-26 22 views

Trả lời

51

Chỉ tìm thấy bài đăng này trong khi cố gắng tìm ra cách để làm điều tương tự. Đây là cách tôi đã làm nó cho bất cứ ai khác tìm thấy điều này trong tương lai.

Phong cách xml như sau:

<?xml version="1.0" encoding="utf-8"?> 
    <resources> 
     <style name="PauseDialog" parent="@android:style/Theme.Dialog"> 
      <item name="android:windowTitleStyle">@style/PauseDialogTitle</item> 
     </style> 

     <style name="PauseDialogTitle" parent="@android:style/TextAppearance.DialogWindowTitle"> 
      <item name="android:gravity">center_horizontal</item> 
     </style> 
     <style name="DialogWindowTitle"> 
     <item name="android:maxLines">1</item> 
     <item name="android:scrollHorizontally">true</item> 
     <item name="android:textAppearance">@android:style/TextAppearance.DialogWindowTitle</item> 
     </style> 
    </resources> 

Và trong hoạt động của tôi onCreateDialog phương pháp cho hộp thoại Tôi muốn theo kiểu tôi có thể tạo hộp thoại như thế này:

Dialog pauseDialog = new Dialog(this, R.style.PauseDialog); 
pauseDialog.setTitle(R.string.pause_menu_label); 
pauseDialog.setContentView(R.layout.pause_menu); 
+0

Tôi đã thử đề xuất của bạn nhưng không đặt tiêu đề cho hộp thoại (HTC Wildfire chạy với Android 2.2.1) ... Bất kỳ ý tưởng nào? Bởi vì trực giác giải pháp của bạn có ý nghĩa :) – Ready4Android

+0

Xin lỗi, không có ý tưởng. Tôi đã thử nghiệm nó trên Ideos của tôi và giả lập với 1,6 và 2,2 không có vấn đề. – ChrisJD

+1

không có "parent =" @ android: style/DialogWindowTitle "". – Hesam

0

Bạn có một số mẹo bắt đầu tại đây để sửa đổi tiêu đề của hộp thoại: Android - change custom title view at run time Không biết nếu nó có thể được căn giữa (chưa thử), nhưng nếu đó là Chế độ xem tùy chỉnh, tôi đoán nó rất có thể .

1

Nếu bạn không gọi AlertDialog.Builder.setIcon()AlertDialog.Builder.setTitle(), thì hộp thoại tùy chỉnh của bạn sẽ không hiển thị Chế độ xem tiêu đề được cài sẵn/mặc định. Trong trường hợp này, bạn có thể thêm tiêu đề tùy chỉnh của mình Xem:

AlertDialog.Builder.setView(View view) 

Ngay khi bạn tạo Chế độ xem này, bạn có thể triển khai bất kỳ loại căn chỉnh nào.

0

Dưới đây là một giải pháp khó chịu ... Mở rộng AlertDialog.Builder và ghi đè lên tất cả các phương thức (ví dụ setText, setTitle, setView, vv) để không thiết lập text/title/view của Dialog thực tế, nhưng để tạo một view mới trong View của Dialog, hãy làm mọi thứ trong đó. Sau đó, bạn được tự do để phong cách tất cả mọi thứ như bạn vui lòng.

Để làm rõ, theo như lớp cha mẹ có liên quan, Chế độ xem được đặt và không có gì khác.

Theo như lớp mở rộng tùy chỉnh của bạn, tất cả mọi thứ được thực hiện trong chế độ xem đó.

85

Một cách khác mà điều này có thể được thực hiện programatically đang sử dụng setCustomTitle():

// Creating the AlertDialog with a custom xml layout (you can still use the default Android version) 
AlertDialog.Builder builder = new AlertDialog.Builder(this); 
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View view = inflater.inflate(R.layout.viewname, null); 
builder.setView(view); 

TextView title = new TextView(this); 
// You Can Customise your Title here 
title.setText("Custom Centered Title"); 
title.setBackgroundColor(Color.DKGRAY); 
title.setPadding(10, 10, 10, 10); 
title.setGravity(Gravity.CENTER); 
title.setTextColor(Color.WHITE); 
title.setTextSize(20); 

builder.setCustomTitle(title); 
+0

'R.layout.viewname' Trong bố cục này, những gì phải được bao gồm ?? chỉ 'TextView' ?? – DroidLearner

+0

Không thể bao gồm toàn bộ phần xem, đây chỉ là nếu bạn muốn tùy chỉnh chế độ xem mặc định của AlertDialog. Tôi tin rằng nó phải là một TextView. –

+0

Tôi nhận được đường viền màu xám cho chế độ xem văn bản này. Làm thế nào để loại bỏ nó? Tôi đã thử \t \t \t title.setBackgroundResource (R.drawable.black_border); Nhưng điều này không giúp được gì. @LandLPartners – user1051505

8

Bạn có thể làm điều đó trong mã là tốt. Giả sử bạn có đoạn hội thoại sau đó thêm các dòng mã sau đây.

@Override 
public void onStart() 
{ 
    super.onStart(); 

    TextView textView = (TextView) this.getDialog().findViewById(android.R.id.title); 
    if(textView != null) 
    { 
     textView.setGravity(Gravity.CENTER); 
    } 
} 
+0

không hoạt động trong hộp thoại appcompatv7 – Asthme

+0

Không hoạt động đối với tôi. findViewById (android.R.id.title); trả về null. – Alyoshak

+0

nó làm việc cho tôi. Tôi đã cố gắng để có được *** TextView *** ngay sau khi *** Dialog *** đối tượng instantiation. –

1

Đối với tùy chỉnh của bạn DialogFragment bạn có thể làm điều này:

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    final Dialog dialog = super.onCreateDialog(savedInstanceState); 
    final TextView textView = (TextView) dialog.findViewById(android.R.id.title); 
    if(textView != null) { 
     textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 
    } 
    return dialog; 
} 
+0

Điều này không phù hợp với tôi. findViewById (android.R.id.title); trả về null. – Alyoshak

0

Hãy thử điều này:

TextView titleText = (TextView) helpDialog.findViewById(R.id.alertTitle); 
if(titleText != null) { 
    titleText.setGravity(Gravity.CENTER); 
} 

Full mã (sử dụng android.support.v7.app.AlertDialog):

AlertDialog.Builder helpDialogBuilder = new AlertDialog.Builder(context) 
     .setTitle(/*your title*/) 
     .setMessage(/*your message*/) 
     .setNegativeButton("Cancel", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         /*you can do something here*/ 

         dialog.dismiss(); 
        } 
       }) 
     .setPositiveButton("OK", 
       new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         /*you can do something here*/ 

         dialog.dismiss(); 
        } 
       }); 

final AlertDialog helpDialog = helpDialogBuilder.create(); 

helpDialog.setOnShowListener(new DialogInterface.OnShowListener() { 
    @Override 
    public void onShow(DialogInterface dialog) { 

     TextView titleText = (TextView) helpDialog.findViewById(R.id.alertTitle); if(titleText != null) { titleText.setGravity(Gravity.CENTER); } 

     TextView messageText = (TextView) helpDialog.findViewById(android.R.id.message); 
     if(messageText != null) { 
      messageText.setGravity(Gravity.CENTER); 
     } 
    } 
}); 

helpDialog.show();
1

Bạn có thể làm điều đó theo chương trình mà không cần xem tùy chỉnh:

@Override 
public void onStart() 
{ 
    super.onStart(); 

    TextView textViewVanilla = (TextView) this.getDialog().findViewById(android.R.id.title); 
    if(textViewVanilla != null) 
    { 
     textViewVanilla.setGravity(Gravity.CENTER); 
    } 
    // support for appcompat v7 
    TextView textViewAppcompat = (TextView) this.getDialog().findViewById(android.support.v7.appcompat.R.id.alertTitle); 
    if(textViewAppcompat != null) 
    { 
     textViewAppcompat.setGravity(Gravity.CENTER); 
    } 
} 

Cảm ơn @hesam cho ý tưởng. Đối với bố cục ứng dụng xem Android/sdk/platforms/android-26/data/res/layout/alert_dialog_title_material.xml

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