我搜寻了很多资料但是找不到方法,如何将ActionBar的标题设置在居中而非左对齐。我使用以下代码将标题设置为居中:
ViewGroup decorView= (ViewGroup) this.getWindow().getDecorView();
LinearLayout root= (LinearLayout) decorView.getChildAt(0);
FrameLayout titleContainer= (FrameLayout) root.getChildAt(0);
TextView title= (TextView) titleContainer.getChildAt(0);
title.setGravity(Gravity.CENTER);
但是它会出现以下错误:
ClassCastException : com.android.internal.widget.ActionBarView can not
be cast to android.widget.TextView.
如有其他解决方案,任何帮助都将不胜感激。