Change title font of Alert Dialog box

后端 未结 14 1980
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-16 10:57

I want to change the font of the title of Alert Dialog box.

Can anybody tell how can I do it?

14条回答
  •  孤街浪徒
    2020-12-16 11:36

    There is an easy way to set a new custom view to dialog's title. We can define every custom view such as TextView and add it some custom properties and at last set it to dialog's title, such as below:

     AlertDialog.Builder builder = new AlertDialog.Builder(OrderItemsActivity.this);
    
    
    
            TextView title_of_dialog = new TextView(getApplicationContext());
            title_of_dialog.setHeight(50);
            title_of_dialog.setBackgroundColor(Color.RED);
            title_of_dialog.setText("Custom title");
            title_of_dialog.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
            title_of_dialog.setTextColor(Color.WHITE);
            title_of_dialog.setGravity(Gravity.CENTER);
    
            builder.setCustomTitle(title_of_dialog);
            builder.create().show();
    

    Here, I define a dynamic TextView and set some properties to it. Finally, I set it to dialog's title using "setCustomTitle()" function.

提交回复
热议问题