【发布时间】: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