android - textview 不服从资源 fontfamily

标签 android android-layout android-fonts android-typeface

我正在使用以这种方式定义的字体类型,android studio 的布局设计 View 以天气图标字体显示字符串/ TextView 。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimaryLight">

    <TextView
        android:id="@+id/tvFragThird"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:fontFamily="@font/weathericons"
        android:text="@string/textview"
        android:textSize="30sp" />
</RelativeLayout>

但是,当我运行这段代码时:

public class ThirdFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_third, container, false);

        TextView tv = (TextView) v.findViewById(R.id.tvFragThird);
//        Typeface typeface = ResourcesCompat.getFont(getContext(), R.font.weathericons);
//        tv.setTypeface(typeface);
        tv.setText("&#xf0eb;");//getArguments().getString("msg"));

return v;
}

我只是得到普通字体的文本。我已经尝试在布局中定义字体系列和在 java 中定义字体,但它们都不起作用。

我错过了什么?

最佳答案

正如这里提到的 https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml ,使用 fontFamily自定义字体,需要 API >= 26

因此,将 TextView 标签更改为 android.support.v7.widget.AppCompatTextView 标签(如果您使用 API < 26)。

我遇到了这个问题,我遇到了上面的链接并做了上面的更改,我的问题已经解决了,我希望它能解决你的(如果还没有解决的话)和其他人的问题。

更新:

正如 RobCo 评论的那样,如果您可以更改为 AppCompatActivity,则不需要进行上述更改。这里提到:https://developer.android.com/reference/android/support/v7/widget/AppCompatTextView

关于android - textview 不服从资源 fontfamily,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48891336/

相关文章:

任务 ':android:mergeReleaseResources' 的 Android Studio 3.5 执行失败

java - Android View 字段名称约定

java - 随机变量导致的运行时错误

android - 使用charcode android检索自定义字符

Android Wear 推荐字体

Android 谷歌地图 apk 显示灰色瓷砖

android - 在 Android 中使用 Sliding TabLayout 时如何更改选项卡中的文本大小和颜色?

android - 传递 Volley 图像作为下一个 Activity 背景图像

android - 是否可以通过前导和跟踪来设置 android 字体样式?

android - OpenCV Android native 代码构建问题