【问题标题】:Custom dialog on Android: How can I center its title?Android 上的自定义对话框:如何将其标题居中?
【发布时间】:2010-10-26 16:11:26
【问题描述】:

我正在开发一个 Android 应用程序。

如何将我正在使用的自定义对话框的标题居中?

【问题讨论】:

    标签: android modal-dialog


    【解决方案1】:

    另一种以编程方式完成的方法是使用 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);
    

    【讨论】:

    • R.layout.viewname 在这个布局中必须包含什么?只有TextView??
    • 不需要包含整个视图部分,这仅适用于您要自定义AlertDialog的默认视图。我确实相信它必须是 TextView。
    • 我得到了这个文本视图的灰色边框。我该如何删除它?我试过 title.setBackgroundResource(R.drawable.black_border);但这无济于事。 @LandLPartners
    • 如果你想从整个 AlertDialog 中删除它,那么你可以参考这个 -stackoverflow.com/questions/8051581/…。
    • 总是最好的答案!
    【解决方案2】:

    刚刚在试图弄清楚如何做同样的事情时发现了这篇文章。以下是我为以后发现此问题的其他人所做的。

    样式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);
    

    【讨论】:

    • 我尝试了您的建议,但它没有将对话框的标题居中(使用 Android 2.2.1 运行的 HTC Wildfire)... 有什么想法吗?因为直觉上你的解决方案很有意义:)
    • 抱歉,不知道。我已经在我的 Ideos 和 1.6 和 2.2 的模拟器上对其进行了测试,没有任何问题。
    • 没有"parent="@android:style/DialogWindowTitle""。
    • 不错的挖掘。您实际上可以将其简化为两层。抱歉格式不好...
    • 这不是正确答案。更正一个由@LandL Partners 完成
    【解决方案3】:

    您也可以在代码中执行此操作。假设您有对话框片段,然后添加以下代码行。

    @Override
    public void onStart()
    {
        super.onStart();
    
        TextView textView = (TextView) this.getDialog().findViewById(android.R.id.title);
        if(textView != null)
        {
            textView.setGravity(Gravity.CENTER);
        }
    }
    

    【讨论】:

    • 也不适合我。 findViewById(android.R.id.title);返回空值。
    • 它对我有用。我试图在 Dialog 对象实例化之后获取 TextView。
    【解决方案4】:

    对于您的自定义 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;
    }
    

    【讨论】:

    • 这对我不起作用。 findViewById(android.R.id.title);返回空值。
    【解决方案5】:

    您可以在没有自定义视图的情况下以编程方式完成:

    @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

    【讨论】:

      【解决方案6】:

      类似于@LandL Partners 解决方案,但在 Kotlin 中:

      val builder = AlertDialog.Builder(this)
      val inflater = this.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
      
      val view = inflater.inflate(R.layout.viewname, null)
      builder.setView(view)
      val title = TextView(this)
      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)
      

      【讨论】:

        【解决方案7】:

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

        AlertDialog.Builder.setView(View view)
        

        只要是您创建此视图,就可以实现任何类型的对齐。

        【讨论】:

          【解决方案8】:
          TextView titleView = (TextView) dialog.findViewById(android.R.id.title);
          if(titleView != null) {
          titleView.setGravity(Gravity.CENTER);
          }
          

          更多详情请见this KodeCenter article on Android Dialog and AlertDialog。

          【讨论】:

            【解决方案9】:

            这里有一些修改对话框标题的技巧: Android - change custom title view at run time 不知道能不能居中(没试过),但是如果是自定义View的话我想是很有可能的。

            【讨论】:

              【解决方案10】:

              这是一个讨厌的解决方案.... 扩展 AlertDialog.Builder 并覆盖所有方法(例如 setText、setTitle、setView 等)以不设置实际 Dialog 的文本/标题/视图,而是在其中创建一个新视图对话框的视图在那里做所有事情。然后,您可以随意设置所有样式。

              澄清一下,就父类而言,视图是设置的,没有别的。

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

              【讨论】:

                【解决方案11】:

                试试这个:

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

                完整代码(使用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(); 

                【讨论】:

                  【解决方案12】:
                      AlertDialog alertDialog = new AlertDialog.Builder(activity)
                  
                              .setMessage(message)
                              .create();
                      alertDialog.setIcon(R.mipmap.ic_launcher_round);
                  
                      @SuppressLint("RestrictedApi")
                      DialogTitle titleView=new DialogTitle(activity);
                      titleView.setText(title);
                      titleView.setPaddingRelative(32,32,32,0);
                      alertDialog.setCustomTitle(titleView);
                  

                  【讨论】:

                    【解决方案13】:

                    在 Kotlin 中,您可以在 1 行中完成

                    dialog!!.window!!.attributes = dialog!!.window!!.attributes.apply { dimAmount = 0F }
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 2011-05-16
                      • 2011-04-23
                      • 1970-01-01
                      • 1970-01-01
                      • 2014-07-29
                      • 1970-01-01
                      • 2010-12-28
                      相关资源
                      最近更新 更多