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

前端 未结 13 1989
后悔当初
后悔当初 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:12

    For your custom DialogFragment you can do this:

    @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 讨论(0)
  • 2020-11-27 13:15

    You've got some starting tips here for modifying the title of a dialog: Android - change custom title view at run time Don't know if it can be centered(haven't tried), but if it's a custom View I guess it's very possible.

    0 讨论(0)
  • 2020-11-27 13:17

    Another way that this can be done programatically is using the 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 讨论(0)
  • 2020-11-27 13:18

    Similar to @LandL Partners solution, but in 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)
    
    0 讨论(0)
  • 2020-11-27 13:19

    Just found this post while trying to figure out how to do the same thing. Here's how I did it for anyone else that finds this in the future.

    Style xml is as follows:

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

    And in my activities onCreateDialog method for the dialog I want styled I create the dialog like this:

    Dialog pauseDialog = new Dialog(this, R.style.PauseDialog);
    pauseDialog.setTitle(R.string.pause_menu_label);
    pauseDialog.setContentView(R.layout.pause_menu);
    
    0 讨论(0)
  • 2020-11-27 13:20
        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);
    
    0 讨论(0)
提交回复
热议问题