Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

android - How to prevent TextView font changing, when we change the Device font style in Samsung devices

I would like say that I have a strange problem in setting textview font. If I change the Device font style from setting-> display, then textview font style also get changed. How can i prevent this?

Here is my textview in my xml layout.

<TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/title_bar_bg"
        android:fontFamily="sans-serif"
        android:typeface="sans"
        android:gravity="center"
        android:text="@string/people_finder"
        android:textColor="@color/WHITE"
        android:textSize="18sp"
        android:textStyle="bold"/>

android:fontFamily="sans-serif" android:typeface="sans"

Also It is working fine in some device like Samsung Galaxy Grand, Samsung Note 2. But not working in Samsung Note 8, Samsung Note 10.1 etc.

is there any other way to do this?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

fontFamily and typeface attributes are related to android native fonts. If you want your TextViews font to be always the same regardless the device font settings, you need to programmatically set a custom Typeface.

Typeface tf = Typeface.createFromAsset(context.getAssets(), "fontName.ttf"));
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setTypeface(tf);

As a side note, Calligraphy project enables actually setting custom font directly from a layout xml.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...