我有一个显示文本视图列表的列表视图。我希望每个文本视图都以适当的字体显示。作为字体字符串[]数组的一部分出现在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呢?
最佳答案
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);