Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未应用自定义字体_Java_Android_Fonts - Fatal编程技术网

Java 未应用自定义字体

Java 未应用自定义字体,java,android,fonts,Java,Android,Fonts,我的自定义字体类 public class CustomFontText extends TextView { /* * Caches typefaces based on their file path and name, so that they don't have to be created every time when they are referenced. */ private static Typeface mTypeface; public

我的自定义字体类

public class CustomFontText extends TextView {
    /*
  * Caches typefaces based on their file path and name, so that they don't have to be created every time when they are referenced.
  */
    private static Typeface mTypeface;

    public CustomFontText(final Context context) {
        super(context, null);
    }

    public CustomFontText(final Context context, final AttributeSet attrs) {
        super(context, attrs, 0);
        readAttrs(context, attrs);
    }

    public CustomFontText(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
        readAttrs(context, attrs);
    }

    private void readAttrs(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView);

        // Read the title and set it if any
        String fontName = a.getString(R.styleable.CustomTextView_fontname);
        if (fontName != null) {
            // We have a attribute value
            if (mTypeface == null) {
                mTypeface = Typeface.createFromAsset(context.getAssets(), fontName);
                setTypeface(mTypeface);
            }
        }

        // a.recycle();
    }
}
在XMl文件中的应用

<somepackage.CustomFontText
            android:id="@+id/details"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ewfewfewqfewfwef"
            custom:fontname="Roboto-Regular.ttf" />


它没有给出任何类型的错误,但我无法在textview中查看任何更改。更改字体名称没有任何区别。

事实上,我不知道为什么您的字体名称不起作用,但是,您也可以使用。我在我的一个项目中使用了它,效果非常好

事实上,我不知道为什么你的不起作用,不过,你也可以使用。我在我的一个项目中使用了它,效果非常好

在项目的资产文件夹中创建文件夹字体,使用文件路径,而不是仅使用自定义:字体名

custom:fontname="fonts/Roboto-Regular.ttf"

在项目的资产文件夹中创建文件夹字体,使用文件路径,而不是仅使用自定义:fontname的字体名称

custom:fontname="fonts/Roboto-Regular.ttf"

在资产文件夹中添加ttf或otf文件

使用TextView创建自定义类扩展

public class CustomText extends TextView {

public CustomText (Context context) {
    super(context);
    createTextView(context, null);
}

public CustomEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    createTextView(context, attrs);
}


public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    createTextView(context, null);
}

private void createTextView(Context context, AttributeSet attrs) {
    String fontName;
    TypedArray typedArray;
    if (isInEditMode())
        return;
    if (attrs != null) {
        typedArray = context.obtainStyledAttributes(attrs, R.styleable.FontTypeFace, 0, 0);
        fontName = typedArray.getString(R.styleable.FontTypeFace_typeface);
        setFontTypeFace(context, fontName);
        typedArray.recycle();
    }
}

private void setFontTypeFace(Context context, String fontName) {
    if (fontName != null) {
        Typeface typeface = Typeface.createFromAsset(context.getAssets(), fontName);
        setTypeface(typeface);
    }
}
}
在attrs文件中声明stylable:

<declare-styleable name="FontTypeFace">
    <attr name="typeface" format="string" />
</declare-styleable>

在xml文件中使用自定义文本视图创建控件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal"
android:weightSum="1">
 <com.Widget.CustomTextView
            android:id="@+id/txt_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/dimen_text_size_12"
            app:typeface="@string/thin" />
</LinearLayout>
<!--String for assets font type file name -->

<string name="bold">bold.otf</string>
<string name="light">light.otf</string>
<string name="medium">medium.otf</string>
<string name="regular">regular.otf</string>
<string name="regular_italic">regular_italic.otf</string>
<string name="semi_bold">semibold.otf</string>
<string name="thin">thin.otf</string>

只需在string.xml文件中添加资产文件名

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal"
android:weightSum="1">
 <com.Widget.CustomTextView
            android:id="@+id/txt_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/dimen_text_size_12"
            app:typeface="@string/thin" />
</LinearLayout>
<!--String for assets font type file name -->

<string name="bold">bold.otf</string>
<string name="light">light.otf</string>
<string name="medium">medium.otf</string>
<string name="regular">regular.otf</string>
<string name="regular_italic">regular_italic.otf</string>
<string name="semi_bold">semibold.otf</string>
<string name="thin">thin.otf</string>

bold.otf
light.otf
中速
普通的
普通斜体
semibold.otf
thin.otf

在资产文件夹中添加ttf或otf文件

使用TextView创建自定义类扩展

public class CustomText extends TextView {

public CustomText (Context context) {
    super(context);
    createTextView(context, null);
}

public CustomEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    createTextView(context, attrs);
}


public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    createTextView(context, null);
}

private void createTextView(Context context, AttributeSet attrs) {
    String fontName;
    TypedArray typedArray;
    if (isInEditMode())
        return;
    if (attrs != null) {
        typedArray = context.obtainStyledAttributes(attrs, R.styleable.FontTypeFace, 0, 0);
        fontName = typedArray.getString(R.styleable.FontTypeFace_typeface);
        setFontTypeFace(context, fontName);
        typedArray.recycle();
    }
}

private void setFontTypeFace(Context context, String fontName) {
    if (fontName != null) {
        Typeface typeface = Typeface.createFromAsset(context.getAssets(), fontName);
        setTypeface(typeface);
    }
}
}
在attrs文件中声明stylable:

<declare-styleable name="FontTypeFace">
    <attr name="typeface" format="string" />
</declare-styleable>

在xml文件中使用自定义文本视图创建控件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal"
android:weightSum="1">
 <com.Widget.CustomTextView
            android:id="@+id/txt_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/dimen_text_size_12"
            app:typeface="@string/thin" />
</LinearLayout>
<!--String for assets font type file name -->

<string name="bold">bold.otf</string>
<string name="light">light.otf</string>
<string name="medium">medium.otf</string>
<string name="regular">regular.otf</string>
<string name="regular_italic">regular_italic.otf</string>
<string name="semi_bold">semibold.otf</string>
<string name="thin">thin.otf</string>

只需在string.xml文件中添加资产文件名

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal"
android:weightSum="1">
 <com.Widget.CustomTextView
            android:id="@+id/txt_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/dimen_text_size_12"
            app:typeface="@string/thin" />
</LinearLayout>
<!--String for assets font type file name -->

<string name="bold">bold.otf</string>
<string name="light">light.otf</string>
<string name="medium">medium.otf</string>
<string name="regular">regular.otf</string>
<string name="regular_italic">regular_italic.otf</string>
<string name="semi_bold">semibold.otf</string>
<string name="thin">thin.otf</string>

bold.otf
light.otf
中速
普通的
普通斜体
semibold.otf
thin.otf

移动代码
设置字体(mTypeface)mTypeface==null
之外的code>应该可以解决问题。因此,代码应该如下所示:

if (mTypeface == null) {
    mTypeface = Typeface.createFromAsset(context.getAssets(), fontName);
}
setTypeface(mTypeface);

这是因为
mTypeface
声明为
static
,并且所有
CustomFontText
实例共享相同的字体(这对于缓存很有意义)。如果在检查中调用了
setTypeface
,则在首次加载字体时,只会应用一次。

移动代码
setTypeface(mTypeface)mTypeface==null
之外的code>应该可以解决问题。因此,代码应该如下所示:

if (mTypeface == null) {
    mTypeface = Typeface.createFromAsset(context.getAssets(), fontName);
}
setTypeface(mTypeface);

这是因为
mTypeface
声明为
static
,并且所有
CustomFontText
实例共享相同的字体(这对于缓存很有意义)。如果在检查中调用了
setTypeface
,则在首次加载字体时,它只会应用一次。

检查此链接:是否检查
fontName
是否为空?你在资产中有字体吗?@Blackbelt我已将所有字体放在我的资产文件夹中。我还调试了我的代码以检查它是否为空。这只是直觉,但你可以尝试移动
setTypeface(mTypeface)mTypeface==null
@Soham很高兴它现在可以工作了!我已将此解决方案作为答案发布。请检查此链接:是否检查
fontName
是否为空?你在资产中有字体吗?@Blackbelt我已将所有字体放在我的资产文件夹中。我还调试了我的代码以检查它是否为空。这只是直觉,但你可以尝试移动
setTypeface(mTypeface)mTypeface==null
@Soham很高兴它现在可以工作了!我已经发布了解决方案作为答案。我不想使用can库。一些代码调整会有帮助。我不想使用can库。一些代码调整会有帮助。什么是自定义\文本视图\字体?什么是自定义\文本视图\字体?