It seems to be broken in Lollipop. I don't know if there is a better alternative, but you can do something like this if you use the toolbar:
import android.content.Context;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import org.apache.commons.lang3.reflect.FieldUtils;
public class MyToolbar extends Toolbar {
protected TextView mmTitleTextView;
public MyToolbar(Context context) {
super(context);
}
public MyToolbar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyToolbar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public TextView getTitleTextView() {
if (mmTitleTextView == null) {
try {
mmTitleTextView = (TextView) FieldUtils.readField(this, "mTitleTextView", true);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return mmTitleTextView;
}
}
And about your activity:
private MyToolbar mActionBarToolbar;
protected Toolbar getActionBarToolbar() {
if (mActionBarToolbar == null) {
mActionBarToolbar = (FWToolbar) findViewById(R.id.toolbar_actionbar);
if (mActionBarToolbar != null) {
setSupportActionBar(mActionBarToolbar);
}
}
return mActionBarToolbar;
}
@Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
getActionBarToolbar();
}
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
if (mActionBarToolbar != null) {
Typeface typeface = Typeface.createFromAsset(c.getAssets(), "fonts/font.ttf");
mActionBarToolbar.getTitleTextView().setTypeface(typeface);
}
}
source
share