【问题标题】:Android navigation icon is not vertically aligned when using custom toolbar使用自定义工具栏时,Android导航图标未垂直对齐
【发布时间】:2020-03-03 16:12:52
【问题描述】:

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

这是自定义工具栏类

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

【问题讨论】:

  • Toolbar 不是已经原生支持 RTL 了吗? imgur.com/a/xccVnhs
  • 可以,但系统语言必须是 RTL 语言之一,并且 android:supportsRtl 必须为真。我想强制工具栏始终是 RTL @Tenfour04

标签: android kotlin


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多