ToolBar修改标题截断位置

Posted by phantomVK on November 28, 2017

通过反射ToolBarTextViewsetEllipsize方法,设置标题中间为截断位置

private void setEllipsize(Toolbar toolBar) {
    try {
        Field field = Toolbar.class.getDeclaredField("mTitleTextView");
        field.setAccessible(true);
        TextView textView = (TextView) field.get(toolBar);
        textView.setEllipsize(TextUtils.TruncateAt.MIDDLE);
    } catch (Exception ignored) {
        Log.e(LOG_TAG, ignored.getMessage());
    }
}