使用自定义工具栏时 Android 导航图标未垂直对齐

Zak*_*was 3 android kotlin

我使用了自定义工具栏类,因此我可以将标题向右对齐,并且除了导航后退图标未垂直对齐外,一切正常

在此输入图像描述

这是自定义工具栏类

class RTLToolbar @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : Toolbar(context, attrs, defStyleAttr) {


    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        super.onLayout(changed, l, t, r, b)
        val childCount = childCount
        for (i in 0 until childCount) {
            val view = this.getChildAt(i)
            if (view is TextView) {
                forceTitleCenter(view,l, r)
                break
            }
        }
    }
    private fun forceTitleCenter(view: TextView, l: Int,  r: Int) {
        val top = view.top
        val bottom = view.bottom
        view.layout(l, top, r, bottom)
        navigationIcon?.let{ view.setPadding(it.intrinsicWidth,0,0,0) }
        view.gravity = Gravity.RIGHT
    }
}
Run Code Online (Sandbox Code Playgroud)

Zak*_*was 10

我找到了这个 xml 属性 app:buttonGravity="center_vertical" ,它完成了工作,现在后退图标与标题对齐