首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过Infalter和TextView视图为此TextView设置TextView?

如何通过Infalter和TextView视图为此TextView设置TextView?
EN

Stack Overflow用户
提问于 2016-03-03 17:43:13
回答 3查看 166关注 0票数 0

如何将操作栏标题的TypeFace设置为此代码?我使用layoutInfalter,但不能为textView设置自定义字体

代码语言:javascript
复制
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);



}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-03-03 17:57:24

在工具栏标题中使用文本视图属性这样声明工具栏:

代码语言:javascript
复制
     <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中

代码语言:javascript
复制
TextView textView = (TextView) findViewById(R.id.action_text);
    textView.setTypeface(Typeface.createFromAsset(this.getAssets(), "fonts/alex.ttf"));
 getSupportActionBar().setDisplayShowTitleEnabled(false);
票数 0
EN

Stack Overflow用户

发布于 2016-03-03 17:55:41

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

代码语言:javascript
复制
TextView txtToolBarTitle = (TextView)   toolbar.findViewById(R.id.toolbar_title);
    txtToolBarTitle.setText(Tit);
    font = Typeface.createFromAsset(getAssets(), "fonts/FORTE.TTF");
    txtToolBarTitle.setTypeface(font);
票数 0
EN

Stack Overflow用户

发布于 2016-03-03 17:59:26

您也可以尝试这样做:

创建一个如下所示的类-

代码语言:javascript
复制
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);
}

}

像下面这样使用这个课程-

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35779270

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档