在Android中制作一个public的打字方法

Make a public type face method in Android

我正在尝试在不同的 Activities 中应用自定义字体,现在我的 MainActivity 中的 onCreate() 中有以下代码:

String fontTitle = "fonts/OpenSans-Bold.ttf";
Typeface titleFont = Typeface.createFromAsset(getAssets(), fontTitle);
page_title.setTypeface(titleFont);

我想知道是否可以制作 Typeface public 以便我可以在其他活动中访问它。

我创建了一个名为 FontHelper 的 class:

public class FontHelper extends MainActivity {

    // path for the fonts
    String fontTitle = "fonts/OpenSans-Bold.ttf";

    Typeface titleFont = Typeface.createFromAsset(getAssets(), fontTitle);

}

但在其他 Activities 中,当我使用 textView.setTypeface(FontHelper.titleFont) 时出现错误。我该如何解决这个错误?

您可以使用静态工厂方法为每个 Activity 创建 Typeface 实例,如下所示:

public class FontHelper {

    private static final String FONT_PATH = "fonts/OpenSans-Bold.ttf";

    public static Typeface getCustomTypeFace(Context context) {
        return Typeface.createFromAsset(context.getAssets(), FONT_PATH);
    }
}

你可以这样使用它:

public class ExampleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final Typeface typeface = FontHelper.getCustomTypeFace(this);
        ...
    }
}

首先,您必须创建一个名为 MasterActivity

的通用 activity
public class MasterActivity extends AppCompatActivity {

    protected Typeface titleFont;

    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);

        // path for the fonts
        String fontTitle = "fonts/OpenSans-Bold.ttf";

        titleFont = Typeface.createFromAsset(getAssets(), fontTitle);
    }
}

然后让您的 MainActivity 像这样从 MasterActivity 扩展:

public class MainActivity extends MasterActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // set the custom font
        page_title.setTypeface(titleFont);
    }
}

并且 AboutActivity 也从 MasterActivity 扩展

public class AboutActivity extends MasterActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);

        // set the custom font
        page_title.setTypeface(titleFont);
    }
}

使用这个自定义字体class

public class TextView extends android.widget.TextView {
    Context mContext;
    String str;
    //fonts
    public static Typeface Font_name;

    public TextView(Context context) {
        super(context);
        mContext=context;


        initialiseFont(null);
    }



    public TextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TextView, 0, 0);
        try {
            str = ta.getString(R.styleable.TextView_font_family);

        } finally {
            ta.recycle();
        }
        initialiseFont(str);
    }

    public TextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext=context;
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TextView, 0, 0);
        try {
            str = ta.getString(R.styleable.TextView_font_family);

        } finally {
            ta.recycle();
        }
        initialiseFont(str);
    }

    public TextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mContext=context;
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TextView, 0, 0);
        try {
            str = ta.getString(R.styleable.TextView_font_family);

        } finally {
            ta.recycle();
        }
        initialiseFont(str);
    }
    private void initialiseFont(String font) {
        if(font==null || font.equals("")){

        }
        else {
            Font_name = Typeface.createFromAsset(mContext.getAssets(), font);
            setTypeface(Font_name);
        }

    }
}

在arrs.xml中添加此标签以读取自定义属性font-family

<resources>
    <declare-styleable name="TextView">
        <attr name="font_family" format="string"/>
    </declare-styleable>
</resources>

将您的字体复制到资产文件夹中(使用相同的文件名),如果您在任何地方使用 TextView,请使用此标签

<Your_package_name_which_you_created_custom_font_class.TextView
        android:text="Hello World!"
        android:layout_width="wrap_content"
        app:font_family="OpenSans-Bold.ttf"
        android:layout_height="wrap_content" />