Java 可扩展列表中的外部字体
我正在尝试在我的一个可扩展列表视图中设置外部字体。我试着像下面一样Java 可扩展列表中的外部字体,java,android,android-typeface,Java,Android,Android Typeface,我正在尝试在我的一个可扩展列表视图中设置外部字体。我试着像下面一样 public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { String headerTitle = (String) getGroup(groupPosition); String headerTitleDate = (
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
String headerTitleDate = (String) getGroupD(groupPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_group, null);
}
Typeface bold = Typeface.createFromAsset(getContext().getAssets(), "shruti.ttf");
TextView listTitle = (TextView) convertView
.findViewById(R.id.tv_listtitle);
TextView listTitleDate = (TextView) convertView
.findViewById(R.id.tv_date);
listTitle.setTypeface(null, Typeface.BOLD);
listTitle.setText(headerTitle);
listTitleDate.setTypeface(null, Typeface.BOLD);
listTitleDate.setText(headerTitleDate);
return convertView;
}
但无法解决getAssets()问题。我曾尝试将其与上下文一起使用,但没有成功。谁能告诉我它有什么问题吗
谢谢执行此操作时,您应该使用视图的上下文,因为它位于适配器内 您可以通过以下方式获取视图的上下文:
view.getContext()
替换此行:
Typeface bold = Typeface.createFromAsset(getContext().getAssets(), "shruti.ttf");
为此:
Typeface bold = Typeface.createFromAsset(convertView.getContext().getAssets(), "shruti.ttf");
将修复错误。希望这有帮助,如果有,请告诉我。我就是这样做的
第1步:
我首先制作了一个可定制的
字体类,用于您使用的shruti.ttf
public class Shruti extends TextView{
public Champagne(Context context) {
super(context);
applyCustomFont(context);
}
public Champagne(Context context, AttributeSet attrs) {
super(context, attrs);
applyCustomFont(context);
}
public Champagne(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
applyCustomFont(context);
}
private void applyCustomFont(Context context) {
Typeface customFont = FontCache.getTypeface("shruti.ttf", context);
setTypeface(customFont);
}
}
第二步:
然后还包括一个FontCache
类
public class FontCache {
private static HashMap<String, Typeface> fontCache = new HashMap<>();
public static Typeface getTypeface(String fontname, Context context) {
Typeface typeface = fontCache.get(fontname);
if (typeface == null) {
try {
typeface = Typeface.createFromAsset(context.getAssets(), fontname);
} catch (Exception e) {
return null;
}
fontCache.put(fontname, typeface);
}
return typeface;
}
}
注:
而不是
<TextView
android:layout_width="match_parent"
android:id="@+id/post_desc"
android:layout_height="wrap_content"
android:text=""
android:textColor="#000"
android:textSize="15dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingBottom="15dp" />
希望它能正常工作这是一个可靠的解决方案为什么不创建一个自定义类,并将其附加到xml代码中的文本视图中呢。我可以用另一种方式来演示roundplz尝试这种字体。createFromAsset(ctx.getAssets(),“fshruti.ttf”)想知道,为什么要使用
listTitle.setTypeface(null,Typeface.BOLD)代码>而不是不使用粗体
?不管怎么说,你的问题是上下文,那么父对象。getContext()
可以做这项工作。你能用line Typeface bold=Typeface.createFromAsset(getContext().getAssets(),“shruti.ttf”)解释一下吗;谢谢
company.override.huzykamz.pixsar.customization is just a package name example , but you just put yours.
<TextView
android:layout_width="match_parent"
android:id="@+id/post_desc"
android:layout_height="wrap_content"
android:text=""
android:textColor="#000"
android:textSize="15dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingBottom="15dp" />