如何将操作栏标题的TypeFace设置为此代码?我使用layoutInfalter,但不能为textView设置自定义字体
String Tit="p1660000";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
setTitle(Tit);
LayoutInflater inflator = LayoutInflater.from(this);
View v = inflator.inflate(R.layout.title_view, null);
((TextView)v.findViewById(R.id.title)).setText(this.getTitle());
Typeface tab= Typeface.createFromAsset(getAssets(), "font/dd.ttf");
getSupportActionBar().setCustomView(v);
}发布于 2016-03-03 17:57:24
在工具栏标题中使用文本视图属性这样声明工具栏:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/action_text"
android:text="Movies List"
android:textStyle="bold"
android:layout_gravity="center"
android:textColor="@color/colorAccent"
android:textSize="40sp"/>
</android.support.v7.widget.Toolbar>在MainActivity.java中
TextView textView = (TextView) findViewById(R.id.action_text);
textView.setTypeface(Typeface.createFromAsset(this.getAssets(), "fonts/alex.ttf"));
getSupportActionBar().setDisplayShowTitleEnabled(false);发布于 2016-03-03 17:55:41
我建议选择一种简单的方法,使用TextView元素创建工具栏的布局文件,然后编写如下代码:
TextView txtToolBarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
txtToolBarTitle.setText(Tit);
font = Typeface.createFromAsset(getAssets(), "fonts/FORTE.TTF");
txtToolBarTitle.setTypeface(font);发布于 2016-03-03 17:59:26
您也可以尝试这样做:
创建一个如下所示的类-
public class CustomTypefaceSpan extends TypefaceSpan{
private final Typeface newType;
public CustomTypefaceSpan(String family, Typeface type) {
super(family);
newType = type;
}
@Override
public void updateDrawState(TextPaint ds) {
applyCustomTypeFace(ds, newType);
}
@Override
public void updateMeasureState(TextPaint paint) {
applyCustomTypeFace(paint, newType);
}
private static void applyCustomTypeFace(Paint paint, Typeface tf) {
int oldStyle;
Typeface old = paint.getTypeface();
if (old == null) {
oldStyle = 0;
} else {
oldStyle = old.getStyle();
}
int fake = oldStyle & ~tf.getStyle();
if ((fake & Typeface.BOLD) != 0) {
paint.setFakeBoldText(true);
}
if ((fake & Typeface.ITALIC) != 0) {
paint.setTextSkewX(-0.25f);
}
paint.setTypeface(tf);
}}
像下面这样使用这个课程-
Typeface font2 = Typeface.createFromAsset(getAssets(), "fonts/<your font in assets folder>");
SpannableStringBuilder SS = new SpannableStringBuilder("MY Actionbar Tittle");
SS.setSpan (new CustomTypefaceSpan("", font2), 0, SS.length(),Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
actionBar.setTitle(ss);https://stackoverflow.com/questions/35779270
复制相似问题