如何通过Infalter为此TextView设置TypeFace并查看?

时间:2016-03-03 17:43:14

标签: android android-actionbar

如何将我的操作栏标题的TypeFace设置为此代码? 我使用layoutInfalter但ican不为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);



}

3 个答案:

答案 0 :(得分:0)

我建议通过使用TextView元素创建工具栏的布局文件然后按如下代码选择一种简单的方法,

TextView txtToolBarTitle = (TextView)   toolbar.findViewById(R.id.toolbar_title);
    txtToolBarTitle.setText(Tit);
    font = Typeface.createFromAsset(getAssets(), "fonts/FORTE.TTF");
    txtToolBarTitle.setTypeface(font);

答案 1 :(得分:0)

使用工具栏标题中所需的文本视图属性声明您的工具栏:

     <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);

答案 2 :(得分:0)

你也可以试试这个:

创建如下所示的类 -

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);