在三星设备中更改设备字体样式时如何防止TextView字体更改

问题描述 投票:8回答:3

我想说我在设置textview字体时遇到了一个奇怪的问题。如果我从设置->显示更改设备字体样式,那么textview字体样式也会更改。我该如何预防?

这是我的xml布局中的textview。

<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">

    </TextView>

android:fontFamily =“ sans-serif”android:typeface =“ sans”

[它在某些设备上也可以正常工作,例如三星Galaxy Grand,三星Note 2。但不适用于Samsung Note 8,Samsung Note 10.1等。

还有其他方法吗?

谢谢

android fonts textview typeface
3个回答
6
投票

fontFamilytypeface属性与android本机字体有关。如果要使TextViews字体始终保持不变,而不考虑设备字体设置,则需要以编程方式设置自定义Typeface

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

作为旁注,Calligraphy项目允许直接从布局xml实际设置自定义字体。


0
投票
public class CustomTextView extends TextView {

    Context context;

    public CustomTextView (Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
    }

    public CustomTextView (Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public CustomTextView (Context context) {
        super(context);
        this.context = context;
    }

    public void setTypeface(Typeface tf, int style) {
        if (style == Typeface.NORMAL) {
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/HelveticaNeue.ttf")/*
                                                                                                             * ,
                                                                                                             * -
                                                                                                             * 1
                                                                                                             */);
        } else if (style == Typeface.ITALIC) {
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/HelveticaNeueItalic.ttf")/*
                                                                                                                 * ,
                                                                                                                 * -
                                                                                                                 * 1
                                                                                                                 */);
        } else if (style == Typeface.BOLD) {
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/HelveticaNeueBold.ttf")/*
                                                                                                                 * ,
                                                                                                                 * -
                                                                                                                 * 1
                                                                                                                 */);
        }
    }

}

并像这样将其添加到您的xml中>]

    <xxx.xxxx.utils.CustomTextView 
        android:id="@+id/login_username_tv"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="0.3"
        android:text="Email"
        android:padding="5dp"
        android:textColor="#333333" 
        android:textSize="12dp"/>
    

0
投票

这在科特林对我有用:

© www.soinside.com 2019 - 2024. All rights reserved.