我想制作一个自定义字体 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/