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
在Android Studio 2.3.3中添加字体_Android_Android Studio_Fonts - Fatal编程技术网

在Android Studio 2.3.3中添加字体

在Android Studio 2.3.3中添加字体,android,android-studio,fonts,Android,Android Studio,Fonts,正如标题所说,我需要使用新字体。我目前使用的是Android Studio 2.3.3 我有一个文件.ttf(字体文件),我想将此字体添加到res目录(/res/font) 我看到了其他问题,但情况似乎有所不同,因为很多人都使用Android Studio 3.0 那么,对于我们这些穷人来说,哪一个过程是正确的呢?1.在资产目录中创建一个新的字体目录,并将您的.ttf字体文件放在这里。 TextView tv = (TextView) findViewById(R.id.tv); Typefac

正如标题所说,我需要使用新字体。我目前使用的是Android Studio 2.3.3

我有一个文件.ttf(字体文件),我想将此字体添加到res目录(/res/font)

我看到了其他问题,但情况似乎有所不同,因为很多人都使用Android Studio 3.0


那么,对于我们这些穷人来说,哪一个过程是正确的呢?

1.在
资产
目录中创建一个新的
字体
目录,并将
您的.ttf
字体文件放在这里。

TextView tv = (TextView) findViewById(R.id.tv);
Typeface tf = Typeface.createFromAsset(v.getContext().getAssets(), "fonts/your.ttf");
tv.setTypeface(tf);
2.您可以更改此选项。

TextView tv = (TextView) findViewById(R.id.tv);
Typeface tf = Typeface.createFromAsset(v.getContext().getAssets(), "fonts/your.ttf");
tv.setTypeface(tf);
编辑

您还可以在xml代码中使用
android:typeface=“sans”
android:typeface=“serif”
android:typeface=“monospace”

TextView tv = (TextView) findViewById(R.id.tv);
Typeface tf = Typeface.createFromAsset(v.getContext().getAssets(), "fonts/your.ttf");
tv.setTypeface(tf);
在xml代码中使用
android:fontFamily

android 4.1/4.2/5.0提供了以下字体系列:

android:fontFamily="sans-serif"           // roboto regular
android:fontFamily="sans-serif-light"     // roboto light
android:fontFamily="sans-serif-condensed" // roboto condensed
android:fontFamily="sans-serif-black"     // roboto black
android:fontFamily="sans-serif-thin"      // roboto thin (android 4.2)
android:fontFamily="sans-serif-medium"    // roboto medium (android 5.0)
也用于样式中

fonts.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="font_family_light">sans-serif-light</string>
    <string name="font_family_medium">sans-serif-medium</string>
    <string name="font_family_regular">sans-serif</string>
    <string name="font_family_condensed">sans-serif-condensed</string>
    <string name="font_family_black">sans-serif-black</string>
    <string name="font_family_thin">sans-serif-thin</string>
</resources>

无衬线灯
无衬线介质
无衬线
无衬线压缩
无衬线黑色
无衬线薄

您可以使用CustomTextView

public class CustomTextView extends TextView {

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init(attrs);
}

public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(attrs);
}

public CustomTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(attrs);
}
public CustomTextView(Context context) {
    super(context);
    init(null);
}

private void init(AttributeSet attrs) {
    if (attrs != null) {
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomTextView);
        String fontName = a.getString(R.styleable.CustomTextView_font);
        try {
            if (fontName != null) {
                Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + fontName);
                setTypeface(myTypeface);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        a.recycle();
    }
}
}

您需要在values文件夹中创建属性

<resources>
<declare-styleable name="CustomTextView">
    <attr name="font" format="string" />
</declare-styleable>

xml中文本视图的后面

<com.packagename.view.CustomTextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="14dp"
    android:textColor="#313131"
    android:textAllCaps="true"
    android:text="Title"
    android:textSize="12sp"
    app:font="@string/monte_semibold"
    />


您在xml中提到的字体名称应该在assets/fonts/folder中提供

您可以尝试以下回答()“我想将此字体添加到res目录(/res/font)”--Android Studio 2.3.3及其相关工具不支持此回答。在向应用程序添加字体时,没有其他方法可以帮助您。是否无法直接在xml文件中更改字体?或者将其设置在样式资源文件中?我喜欢这个解决方案,因为它允许“将字体名称作为参数传递”,可以这样说。谢谢