java - 自定义字体在 Fragment Android Studio 中不起作用

标签 java android android-layout android-fragments

我想制作一个自定义字体 TextView 。

我已经进入FragEarnCredits.java:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (aiView == null) {
            aiView = inflater.inflate(R.layout.fragment_earn_credits, container, false);
        }


        t = (TextView) aiView.findViewById(R.id.logo);
        Typeface myCustomFont=Typeface.createFromAsset(aiContext.getAssets(),"fonts/bebas.otf");
        t.setTypeface(myCustomFont);

fragment_earn_credits.xml中:

<TextView
    android:id="@+id/logo"
    android:text="Fashion Wallet"
    android:gravity="center"
    android:textSize="40dp"
    android:textColor="@color/md_brown_700"
    android:layout_marginTop="100dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
  />

我收到了空错误。为什么?请大家帮帮我!

#编辑

我尝试改变:

 Typeface myCustomFont=Typeface.createFromAsset(aiContext.getAssets(),"fonts/bebas.otf");

Typeface myCustomFont = Typeface.createFromAsset(getActivity().getAssets(), "fonts/bebas.otf");

0 个错误,但字体没有改变。

最佳答案

Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/customfont.ttf");

您在 fragment 类中发送的上下文,您必须使用您的实际上下文。

希望对您有帮助!干杯!

关于java - 自定义字体在 Fragment Android Studio 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40574079/

相关文章:

Android:自定义Toast通知继承默认Toast

java - 如何在 Spring-Boot 中注册启用 "async-supported"的 servlet?

java - 使用 Java 代码访问 Tomcat 监视器

java - 将 ActionListener() 添加到 Java 中的 JButton-Array : compilation error

android - 模拟器中蓝牙设备支持的预后如何?

Android:关闭软键盘时显示空白

java - 将时间作为字符串放入查询中

android - 当我进行 repo 同步时,幕后会发生什么?

android - 在水平线性布局中,如何从左到右而不是从右到左添加

java - 为什么Gson库在调试时显示空值?