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

前端 未结 13 2141
后悔当初
后悔当初 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: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);
    

提交回复
热议问题