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

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

提交回复
热议问题