我正在使用以这种方式定义的字体类型,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("");//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/