以编程方式设置自定义字体

时间:2015-12-14 00:19:19

标签: java android textview android-typeface

相当直截了当。它似乎不想为我工作。我得到的错误是getAssets()为红色,表示Cannot resolve method 'getAssets()'

    text_View = (TextView) this.view.findViewById(R.id.textDesigner);
    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/Calligraffitti-Regular.ttf");
    text_View.setTypeface(myTypeface);

我的字体路径为:C:\Users\Joe\AndroidStudioProjects\my-app\app\src\main\assets\fonts\Calligraffitti-Regular.ttf

发生了什么事?

4 个答案:

答案 0 :(得分:5)

尝试调用片段的活动,然后调用资产

getActivity().getAssets()

答案 1 :(得分:4)

如果你在片段替换

Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/Calligraffitti-Regular.ttf");

Typeface myTypeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Calligraffitti-Regular.ttf");

干杯

答案 2 :(得分:0)

此对象不是指您的活动。使用

WhatevernameActivity.this.getAssets();

答案 3 :(得分:0)

private static void selectionSort(int fromIndex, int[] array) {
    int currentMin = array[fromIndex];
    int indexOfMin = fromIndex;

    for(int j = fromIndex+1; j < array.length; j++) {
        if(array[j] < currentMin) {
            currentMin = array[j];
            indexOfMin = j;
        }
    }
    swap(array, fromIndex, indexOfMin);
}

private static void swap(int[] a, int i, int j) {
    int temp = a[i];
    a[i] = a[j];
    a[j] = temp;
}

在此mettod中传递Context和textview