我正在尝试创建一个返回titleFont和contentFont字体的方法,以便通过返回引用和减少代码长度来提高效率。我知道这很简单,但我没有办法。任何人都可以帮忙。或任何其他选择将被赞赏..抱歉英语不好
以下是将要运行多次的方法..
protected void onPostExecute(Article result) {
super.onPostExecute(result);
TextView txtTitle= (TextView) view.findViewById(R.id.title);
txtTitle.setTypeface(titleFont);
txtTitle.setText(result.getTitle());
private Typeface titleFont=Typeface.createFromAsset(context.getAssets(),"fonts/InterstateCondMonoLgt.ttf");
private Typeface contentFont=Typeface.createFromAsset(context.getAssets(),"fonts/InterstateLight.ttf");
TextView txtMain= (TextView) view.findViewById(R.id.main);
txtMain.setTypeface(contentFont);
txtMain.setText(result.getContent());
}
答案 0 :(得分:6)
我希望这可以帮到你;)
FontUtil类
public class FontUtil {
private static Typeface mTitleFont;
private static Typeface mContentFont;
public static enum FontType {
TITLE_FONT {
public String toString() {
return "fonts/InterstateCondMonoLgt.ttf";
}
},
CONTENT_FONT {
public String toString() {
return "fonts/InterstateLight.ttf";
}
}
}
/**
* @return Typeface Instance with the font passed as parameter
*/
public static Typeface getTypeface(Context context, String typefaceName) {
Typeface typeFace = null;
try {
if (typefaceName.equals(FontType.TITLE_FONT.toString())) {
if (mTitleFont == null) {
mTitleFont = Typeface.createFromAsset(context.getAssets(), typefaceName);
}
typeFace = mTitleFont;
} else if (typefaceName.equals(FontType.CONTENT_FONT.toString())) {
if (mContentFont == null) {
mContentFont = Typeface.createFromAsset(context.getAssets(), typefaceName);
}
typeFace = mContentFont;
}
} catch (Exception ex) {
typeFace = Typeface.DEFAULT;
}
return typeFace;
}
/**
* @return Typeface Instance with the font passed as parameter
*/
public static Typeface getTypeface(Context context, FontType typefaceName) {
return getTypeface(context, typefaceName.toString());
}
}
<强>客户端强>
protected void onPostExecute(Article result) {
super.onPostExecute(result);
TextView txtTitle= (TextView) view.findViewById(R.id.title);
txtTitle.setTypeface( FontUtil.getTypeface(mContext, FontType.TITLE_FONT) );
txtTitle.setText(result.getTitle());
TextView txtMain= (TextView) view.findViewById(R.id.main);
txtMain.setTypeface( FontUtil.getTypeface(mContext, FontType.CONTENT_FONT) );
txtMain.setText(result.getContent());
}
答案 1 :(得分:1)
您可以使用
在项目中创建一个实用程序类private Typeface titleFont=Typeface.createFromAsset(context.getAssets(),"fonts/InterstateCondMonoLgt.ttf");
private Typeface contentFont=Typeface.createFromAsset(context.getAssets(),"fonts/InterstateLight.ttf");
然后简单地为它写geter
public Typeface getTitleFont() {
return titleFont;
}
或者我更喜欢的,如果您继承了基类中的类,如:
public static Typeface titleFont;
然后
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
titleFont=Typeface.createFromAsset(context.getAssets(),"fonts/InterstateCondMonoLgt.ttf");
}
这样你就会有字体
只需在您需要的地方拨打textView.setTypeface(titleFont);
答案 2 :(得分:0)
在特定方法中创建的所有变量本身都是私有的。 您无法为方法中声明的任何变量提供访问修饰符。它会给你编译错误。 我建议你将这个typefaces变量声明为类级变量,并在ASYNCTASK的构造函数中初始化它们。否则,每次调用onPostExecute()时,每次都会创建一个字体,这可能会在以后的某个时间点引起内存开销。
答案 3 :(得分:0)
创建一些util类并将所有代码放在那里:
public class Utils
{
public static Typeface getTitleFont()
{
return Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/InterstateCondMonoLgt.ttf");
}
}
并像这样使用:
TextView txtMain= (TextView) view.findViewById(R.id.main);
txtMain.setTypeface(your_package.Utils.getTitleFont());