在Android中创建一个公共类型的face方法

时间:2016-05-04 02:35:24

标签: java android public-method

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

MainActivity

我想知道是否可以将String fontTitle = "fonts/OpenSans-Bold.ttf"; Typeface titleFont = Typeface.createFromAsset(getAssets(), fontTitle); page_title.setTypeface(titleFont); 公开,以便我可以在其他活动中访问它。

我创建了一个名为Typeface的类:

FontHelper

但在其他public class FontHelper extends MainActivity { // path for the fonts String fontTitle = "fonts/OpenSans-Bold.ttf"; Typeface titleFont = Typeface.createFromAsset(getAssets(), fontTitle); } 使用Activities时,我收到错误消息。我该如何解决这个错误?

3 个答案:

答案 0 :(得分:1)

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

Activity

你可以像这样使用它:

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

答案 1 :(得分:-1)

首先,您必须创建一个名为MasterActivity的公共活动

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

答案 2 :(得分:-1)

使用此自定义字体类

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