我有一个显示TextView列表的ListView。我希望每个TextView以适当的字体显示。作为字体String []数组的一部分出现在ListView中的字体名称拼写与创建字体时的字体名称相同。
ListView fontsListView = (ListView) this.view.findViewById(R.id.MenuLayout);
final String[] fonts = new String[] {
"Aclonica",
"Amino-Regular",
};
ArrayList<String> fontsList = new ArrayList<String>();
fontsList.addAll( Arrays.asList(fonts) );
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(getActivity(), R.layout.fonts_simple_row, fontsList);
final TextView[] textViewArray = new TextView[0];
for( int i = 0; i <= fonts.length - 1; i++) {
textViewArray[i].setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "fonts/" + fonts[i] + ".ttf"));
}
fontsListView.setAdapter(listAdapter);
和XML:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:id="@+id/rowTextView"
android:textSize="20sp"
android:gravity="center_horizontal"
android:padding="10dp"
android:typeface="sans"
android:fontFamily="sans-serif-light" />
为什么我不能让ListView中的每个TextView成为正确的Typface?
答案 0 :(得分:3)
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.fonts_simple_row,value) {
@Override
public View getView(int position, View convertView,ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view.findViewById(R.id.rowTextView);
textView.setTypeface(Typeface.createFromAsset(getActivity().getAssets(),"fonts/" + fonts[position] + ".ttf"));
return view;
}
};
fontsListView.setAdapter(arrayAdapter);
答案 1 :(得分:3)
您应该使用自定义数组适配器并在get view方法上设置字体。
public string GetFindBooks(string author="", string title="", string isbn="", string somethingelse="", DateTime? date= null)
{
// ...
}
答案 2 :(得分:1)
只创建一次字体并使用它。避免在getview方法中创建字体。如果不保持可重用性,每次滚动时都会创建字体。
使用ViewHolder模式在listview中获得更好的性能
下面我试着给出两件事。我也在评论中给出了解释。请检查它是否对您有帮助。
public class AdapterFontViewHolder extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
Typeface typefaceArial, typefaceRoboto;
private Typeface[] fonts;
public AdapterFontViewHolder(Context context, String[] values) {
super(context, R.layout.rowlayout, values);
this.context = context;
this.values = values;
// Create Typeface only once and use it.. Path you can change as per your directory
fonts = new Typeface[]{
Typeface.createFromAsset(context.getAssets(), "fonts/Aclonica.ttf"),
Typeface.createFromAsset(context.getAssets(), "fonts/Amino-Regular.ttf"),
};
}
public class ViewHolder {
TextView textView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vHolder;
if (convertView == null) {
// Create Instnce of view if its null & store object in ViewHolder (a class)
vHolder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.rowlayout, parent, false);
vHolder.textView = (TextView) convertView.findViewById(R.id.label);
//Set ViewHolder instance in convertview in a tag as a object
convertView.setTag(vHolder);
} else {
// reuse as already converview is instansiated & it holds ViewHolder instance in tag
vHolder = (ViewHolder) convertView.getTag();
}
vHolder.textView.setText(values[position]);
//Here From Fonts array take Typeface
vHolder.textView.setTypeface(fonts[position]);
return convertView;
}
}
设置适配器
ListView fontsListView = (ListView) this.view.findViewById(R.id.MenuLayout);
AdapterFontViewHolder listAdapter = new AdapterFontViewHolder(getActivity(), R.layout.fonts_simple_row, fontsList);
fontsListView.setAdapter(listAdapter);