Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.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 如何从TextView获取fontFamily名称?_Android_Textview_Android Typeface - Fatal编程技术网

Android 如何从TextView获取fontFamily名称?

Android 如何从TextView获取fontFamily名称?,android,textview,android-typeface,Android,Textview,Android Typeface,我想从代码中的xml中获取字体族名称attr name。 例如,我有自定义textView类: public class TextVieww extends TextView{ public TextVieww(Context context) { super(context); } public TextVieww(Context context, AttributeSet attrs) { super(context, attrs);

我想从代码中的xml中获取字体族名称attr name。 例如,我有自定义textView类:

public class TextVieww extends TextView{

    public TextVieww(Context context) {
        super(context);
    }
    public TextVieww(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public TextVieww(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public void init(Context mContext ,TextView view) {
    }
}
XML:


我想从textView.class获取“sans serif thin”。
这是可能的吗?如何做到这一点?thank's

如果字体系列名称是在XML中定义的,则无法通过编程方式获取该名称,因为在XML中定义时,它在编译时映射到关联的本机字体系列,并且无法收回,而不会产生一些丑陋的反射(我将试图找到上述声明的来源,以获得完整的答案,字体文档似乎有限)

正如@Ashwini在评论中提到的,您可以始终在assets文件夹下使用自定义字体,并且可以在XML文件和.java中看到它

或者,如果您想使用本机字体系列,您可以做一些更简单、不美观的事情;使用TextView的android:tag字段存储字体系列名称:

在XML中:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:id='@+id/textView'
    android:textSize="30sp"
    android:textStyle="bold"
    android:fontFamily="@string/myFontFamily"
    android:tag="@string/myFontFamily"
/>

如果字体系列名称是用XML定义的,则无法通过编程方式获取它,因为在XML中定义时,它在编译时映射到关联的本机字体系列,并且无法收回,而不会产生一些丑陋的反射(为了获得完整的答案,我将尝试找到上述声明的来源,字体文档似乎是有限的)

正如@Ashwini在评论中提到的,您可以始终在assets文件夹下使用自定义字体,并且可以在XML文件和.java中看到它

或者,如果您想使用本机字体系列,您可以做一些更简单、不美观的事情;使用TextView的android:tag字段存储字体系列名称:

在XML中:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:id='@+id/textView'
    android:textSize="30sp"
    android:textStyle="bold"
    android:fontFamily="@string/myFontFamily"
    android:tag="@string/myFontFamily"
/>

将font-family.ttf文件放在资产文件夹中,在textView.class上给出该链接并在xml上调用该链接我只需要字体家族的当前名称,而不是替换itetXtView.setTypeface(Typeface.create(“sans serif thin”,Typeface.NORMAL));像这样吗?@Anna尝试喜欢
tv\u home\u putAwqy.getTypeface()
将font-family.ttf文件放在资产文件夹中,在textView.class上给出该链接并在xml上调用该链接我只想知道字体家族的当前名称,而不是替换ittextView.setTypeface(Typeface.create(“sans serif thin”,Typeface.NORMAL));像这样吗?@Anna试着喜欢
tv\u home\u putAwqy.getTypeface()
<resources>
    ...
    <string name="myFontFamily">sans-serif-thin</string>
    ...
</resources>
TextView textView = (TextView) findViewById(R.id.textView);
String fontFamily = String.valueOf(textView.getTag());