Android 如何在片段布局中使用自定义字体?
*编辑 我认为它与安卓5.0.2有关。我尝试了一个活动,它给了我同样的错误 所以我对导航抽屉和碎片还不熟悉。一切正常,现在我想有一个自定义字体的文本视图。通常它使用下面的方法工作,但现在不行Android 如何在片段布局中使用自定义字体?,android,android-fragments,custom-font,Android,Android Fragments,Custom Font,*编辑 我认为它与安卓5.0.2有关。我尝试了一个活动,它给了我同样的错误 所以我对导航抽屉和碎片还不熟悉。一切正常,现在我想有一个自定义字体的文本视图。通常它使用下面的方法工作,但现在不行 public class HomeFragment extends Fragment { public HomeFragment(){} @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
public class HomeFragment extends Fragment {
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// I use this everytime for normal layouts, but with a fragment layout it doesnt work
TextView txt = (TextView) findViewById(R.id.textViewHome);
Typeface font = Typeface.createFromAsset(getAssets(), "impact.ttf");
txt.setTypeface(font);
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
}
我得到的错误是:
The method getAssets() is undefined for the type HomeFragment
The method findViewById(int) is undefined for the type HomeFragment
由于我是android编程新手,我真的很想找到一个解决方案,并想了解为什么它是错误的。非常感谢您的帮助 你只需要改变一些事情。您需要先膨胀视图,然后再获取其子视图,还需要获取获取资产的活动:
public class HomeFragment extends Fragment {
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
TextView txt = (TextView) rootView.findViewById(R.id.textViewHome);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "impact.ttf");
txt.setTypeface(font);
return rootView;
}
}
你只需要改变一些事情。您需要先膨胀视图,然后再获取其子视图,还需要获取获取资产的活动:
public class HomeFragment extends Fragment {
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
TextView txt = (TextView) rootView.findViewById(R.id.textViewHome);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "impact.ttf");
txt.setTypeface(font);
return rootView;
}
}
只要改变一下就行了
public class HomeFragment extends Fragment {
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView txt = (TextView) findViewById(R.id.textViewHome);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "impact.ttf");
txt.setTypeface(font);
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
}
这里getActivity将为您提供上下文,您可以从该上下文访问assets文件夹。它会起作用的:只要做这个改变,它就会起作用
public class HomeFragment extends Fragment {
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView txt = (TextView) findViewById(R.id.textViewHome);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "impact.ttf");
txt.setTypeface(font);
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
}
这里getActivity将为您提供上下文,您可以从该上下文访问assets文件夹。它将起作用:尝试将s添加到字体,如资产/字体尝试将s添加到字体,如资产/字体@DyRight-如果答案正确,请接受答案:由于某种原因,当我尝试运行应用程序时,它会给我一个错误字体“找不到”。这与新的android棒棒糖有关吗?我以前从未犯过这样的错误。现在尝试了多种字体,仍然无效。字体是否在资产文件夹中?是的,资产/fonts/impact.ttfLet us.@DyRight-如果答案正确,请接受答案:由于某种原因,当我尝试运行应用程序时,它会给我一个错误字体“找不到”。这与新的android棒棒糖有关吗?我以前从未犯过这样的错误。现在尝试了多种字体,仍然不好。字体是否在assets文件夹中?是的,assets/fonts/impact.ttfLet us。它仍然给我错误:找不到字体,但字体在assets/font文件夹中,名称正确。我也尝试了不同的字体,但它们都给我相同的错误。它仍然给我错误:找不到字体,但字体在资产/字体文件夹中,名称正确。我也尝试了不同的字体,但它们都给了我相同的错误。