Custom dialog on Android: How can I center its title?

前端 未结 13 1990
后悔当初
后悔当初 2020-11-27 12:43

I\'m developing an Android application.

How can I center the title for a custom dialog that I\'m using?

相关标签:
13条回答
  • 2020-11-27 13:36

    You can do it programmatically without custom view:

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

    Thanks @hesam for the idea. For appcompat layout see Android/sdk/platforms/android-26/data/res/layout/alert_dialog_title_material.xml

    0 讨论(0)
提交回复
热议问题