Change title font of Alert Dialog box

后端 未结 14 1933
佛祖请我去吃肉
佛祖请我去吃肉 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

    You have to inflate or customize and create a style and apply to AlertDialog

    Heres how you inflate a layout and apply it to AlertDialog

    LayoutInflater li = LayoutInflater.from(ctx);
    View view = li.inflate(R.layout.formatted_dialog, null);
    
    AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
    builder.setTitle("Formatted");
    builder.setView(view);
    

    define all the formatting and styles required in the layout you specified.

    You can access specific textview defined in the layout using inflated View i.e.

    LayoutInflater li = LayoutInflater.from(ctx);
    View view = li.inflate(R.layout.formatted_dialog, null);
    TextView label=(TextView)view.findViewById(R.id.i_am_from_formatted_layout_lable);
    

    Sample layout saved as res/layout/link.xml:

    In your onCreate() or where or whenever you want to call AlertDialog

    LayoutInflater li = LayoutInflater.from(this);
    View view = li.inflate(R.layout.link, null);
    
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Formatted");
    builder.setView(view).create().show();
    TextView text=(TextView) findViewById(R.id.text);
    

    replace this with context object if you are calling from some other method.

提交回复
热议问题