上述问题只出现在 Lollipop 版本中,Kitkat 及更低版本中不会出现。Lollipop 并没有按照文字的样式,而把它自己的TitleTextAppearance
. 因此,即使您为您添加自己的TextView
孩子Toolbar
(如下所示),android.support.v7.widget.Toolbar
仍然会覆盖您的样式。
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:theme="@style/someToolbarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
要最终解决这个问题,请将您自己TextView
的标题放在标题中并覆盖 的setTitle
方法Toolbar
以跳过 Lollipop 的预定义TitleTextAppearance
.
public class YourCustomToolbar extends Toolbar {
public YourCustomToolbar (Context context) {
super(context);
}
public YourCustomToolbar (Context context, AttributeSet attrs) {
super(context, attrs);
}
public YourCustomToolbar (Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setTitle(CharSequence title) {
((TextView) findViewById(R.id.title)).setText(title);
}
@Override
public void setTitle(int title) {
((TextView) findViewById(R.id.title)).setText(title);
}
}