Android 如何通过Infalter和view设置此文本视图的字体?
如何将我的操作栏标题的字体设置为此代码? 我使用的是layoutInfalter,但无法为textView设置自定义字体Android 如何通过Infalter和view设置此文本视图的字体?,android,android-actionbar,Android,Android Actionbar,如何将我的操作栏标题的字体设置为此代码? 我使用的是layoutInfalter,但无法为textView设置自定义字体 String Tit="p1660000"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolba
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);
}
我建议选择一种简单的方法,创建一个包含TextView元素的工具栏布局文件,然后编写如下代码
TextView txtToolBarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
txtToolBarTitle.setText(Tit);
font = Typeface.createFromAsset(getAssets(), "fonts/FORTE.TTF");
txtToolBarTitle.setTypeface(font);
使用工具栏标题中所需的文本视图属性如下所示声明工具栏:
<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>
您也可以尝试以下方法:
创建一个如下所示的类-
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);
}
使用下面的类-
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);
Typeface font2=Typeface.createFromAsset(getAssets(),“fonts/”);
SpannableStringBuilder SS=新的SpannableStringBuilder(“我的Actionbar标题”);
SS.setSpan(新的自定义字体SPAN(“,font2),0,SS.length(),SPAN.SPAN(不含);
actionBar.setTitle(ss);
请给我一个强制关闭的命令好的,我发现问题我的资产目录没有字体文件夹,因为我的字体目录名是字体谢谢