2010-10-26 29 views

回答

51

刚刚发现这篇文章,同时试图找出如何做同样的事情。以下是我为未来发现这一点的其他人做过的事情。

风格的XML如下:

<?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> 

在我的活动onCreateDialog因为我想要的对话框方法风格创建对话框是这样的:

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

我试过了你的建议,但它没有将对话框的标题居中(HTC Wildfire与Android 2.2.1一起运行)......任何想法?因为直观上你的解决方案很有意义:) – Ready4Android 2011-08-28 23:47:42

+0

对不起,不知道。我已经在我的Ideos和1.6和2.2的模拟器上测试过了,没有任何问题。 – ChrisJD 2011-10-16 00:28:08

+1

没有“parent =”@ android:style/DialogWindowTitle“”。 – Hesam 2011-11-30 10:35:31

1

如果您没有拨打AlertDialog.Builder.setIcon()AlertDialog.Builder.setTitle(),那么您的自定义对话框将不会显示内置/默认标题View。在这种情况下,您可以添加自定义标题视图:

AlertDialog.Builder.setView(View view) 

只要它是你谁创建这个视图中,可以实现任何类型的对齐。

0

这里是一个讨厌的解决方案...扩展AlertDialog.Builder并覆盖所有的方法(例如,setText,setTitle,setView等),以便不设置实际的Dialog文本/标题/视图,而是在Dialog的View中创建一个新的视图。那么你可以随心所欲地设计任何样式。

为了阐明,就父级而言,设置了视图,没有别的。

就您的自定义扩展类而言,一切都在该视图内完成。

85

的另一种方式,这可以通过编程使用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'在此布局中必须包含哪些内容?只有'TextView'? – DroidLearner 2012-12-22 03:28:20

+0

整个视图部分不需要包含,只有当您想要自定义AlertDialog的默认视图时才需要包含整个视图部分。我确信它必须是一个TextView。 – 2012-12-23 13:55:38

+0

我得到这个textview的灰色边框。我如何删除它?我试过\t \t \t title.setBackgroundResource(R.drawable.black_border);但是这没有帮助。 @LandLPartners – user1051505 2013-12-07 07:29:46

8

你可以做到这一点的代码。假设你有对话框片段,然后添加下面的代码行。

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

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

在appcompatv7对话框中不起作用 – Asthme 2015-12-03 07:43:57

+0

不适用于我。 findViewById(android.R.id.title);返回null。 – Alyoshak 2016-11-28 22:30:09

+0

它为我工作。我试图在***对话框***对象实例化之后获得*** TextView ***。 – 2017-02-10 18:32:50

1

为您定制DialogFragment你可以这样做:

@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

这对我不起作用。 findViewById(android.R.id.title);返回null。 – Alyoshak 2016-11-28 22:31:47

1
TextView titleView = (TextView) dialog.findViewById(android.R.id.title); 
if(titleView != null) { 
titleView.setGravity(Gravity.CENTER); 
} 

详情请参阅this KodeCenter article on Android Dialog and AlertDialog。(使用android.support.v7.app.AlertDialog

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

的完整代码:

0

试试这个

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

您可以通过编程做没有自定义视图:

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

感谢@hesam为理念。对于appcompat布局,请参见Android/sdk/platforms/android-26/data/res/layout/alert_dialog_title_material.xml

相关问题