使用自定义工具栏时,Android 导航图标垂直对齐存在问题。

3
我使用了自定义的工具栏类,以便将标题对齐到右侧,一切正常,除了导航返回图标没有垂直对齐。

enter image description here

这是自定义工具栏类

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
    }
}

工具栏本身不支持RTL吗?https://imgur.com/a/xccVnhs - Tenfour04
它可以,但系统语言必须是从右到左的一种语言,并且android:supportsRtl必须为true。我想要强制工具栏始终从右到左显示。@Tenfour04 - Zakaria M. Jawas
1个回答

10
我发现这个XML属性app:buttonGravity="center_vertical",它可以实现对齐效果,现在返回图标与标题对齐了。

在我浏览器中打开了大约20个标签后,终于 :) 谢谢 - m.sajjad.s
@m.sajjad.s 很高兴能帮到你,愿你编程愉快。 - Zakaria M. Jawas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接