Saq*_*eib 3 java android textview android-adapter font-awesome-4
我创建了一个自定义TextView来使用Font Awesome支持,当你在layout xml中添加文本(unicode )时它工作得很好.但如果我试图动态使用我的适配器设置文本view.setText(),它不应用字体.
FontView类
public class FontView extends TextView {
private static final String TAG = FontView.class.getSimpleName();
//Cache the font load status to improve performance
private static Typeface font;
public FontView(Context context) {
super(context);
setFont(context);
}
public FontView(Context context, AttributeSet attrs) {
super(context, attrs);
setFont(context);
}
public FontView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setFont(context);
}
private void setFont(Context context) {
// prevent exception in Android Studio / ADT interface builder
if (this.isInEditMode()) {
return;
}
//Check for font is already loaded
if(font == null) {
try {
font = Typeface.createFromAsset(context.getAssets(), "fontawesome-webfont.ttf");
Log.d(TAG, "Font awesome loaded");
} catch (RuntimeException e) {
Log.e(TAG, "Font awesome not loaded");
}
}
//Finally set the font
setTypeface(font);
}
}
Run Code Online (Sandbox Code Playgroud)
XML for Layout
<com.domain.app.FontView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="60sp"
android:textAlignment="center"
android:text=""
android:gravity="center"
android:id="@+id/iconView"
android:background="@drawable/oval"
android:padding="10dp"
android:layout_margin="10dp" />
Run Code Online (Sandbox Code Playgroud)
我的适配器
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
IconListHolder viewHolder;
if( convertView == null ) {
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = layoutInflater.inflate(R.layout.icon, null);
viewHolder = new IconListHolder(v);
v.setTag(viewHolder);
} else {
viewHolder = (IconListHolder) v.getTag();
}
//Set the text and Icon
viewHolder.textViewIcon.setText(pages.get(position).getIcon());
viewHolder.textViewName.setText(pages.get(position).getTitle());
return v;
}
private class IconListHolder {
public FontView textViewIcon;
public TextView textViewName;
public IconListHolder(View base) {
textViewIcon = (FontView) base.findViewById(R.id.iconView);
textViewName = (TextView) base.findViewById(R.id.iconTextView);
}
}
Run Code Online (Sandbox Code Playgroud)
请帮忙我做错了什么.
经过3个多小时的挣扎.我在这里找到了答案.这是unicode问题.
更改适配器setText()以使用Html.fromHtml("").toString()修复问题
viewHolder.textViewIcon
.setText(Html.fromHtml(pages.get(position).getIcon()).toString());
Run Code Online (Sandbox Code Playgroud)
我希望你们发现它有用.
| 归档时间: |
|
| 查看次数: |
2028 次 |
| 最近记录: |