
我有2个XML文件列表视图.一个用于视图,另一个用于第一个.
note_List.XML是:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" androID:padding="10dip" > <button androID:ID="@+ID/allNotes_btn_refresh_List" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:text="@string/all_note_refresh_List" /> <ListVIEw androID:ID="@androID:ID/List" androID:layout_wIDth="fill_parent" androID:layout_height="match_parent" /></linearLayout>和List_otem.XML是:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" > <TextVIEw androID:ID="@+ID/List_lbl_ID" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:visibility="gone" /> <TextVIEw androID:ID="@+ID/List_lbl_subject" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:textAppearance="?androID:attr/textAppearanceLarge" /> <TextVIEw androID:ID="@+ID/List_lbl_date" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:textAppearance="?androID:attr/textAppearanceSmall" /></linearLayout>在下面的代码中,我为列表设置了适配器:
ArrayList<HashMap<String, String>> noteList;ListVIEw lv;@OverrIDepublic voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.note_List); noteList = new ArrayList<HashMap<String, String>>(); lv = getListVIEw(); lv.setonItemClickListener(new OnItemClickListener() { public voID onItemClick(AdapterVIEw<?> parent, VIEw vIEw, int position, long ID) { String note_ID = ((TextVIEw) vIEw .findVIEwByID(R.ID.List_lbl_ID)).getText().toString(); Intent i = new Intent(getApplicationContext(), NoteDetail.class); i.putExtra("note_ID", note_ID); startActivityForResult(i, 100); } });}public class LoadAllNotes extends AsyncTask<String, String, String> { @OverrIDe protected voID onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(AllNotes.this); pDialog.setMessage("???? ??? ????..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); noteList.clear(); } protected String doInBackground(String... args) { UserFunctions userFunctions = new UserFunctions(); Json = userFunctions.getAllNotes(userID); Log.i("AllNotes >> Json >>", Json.toString()); return null; } protected voID onPostExecute(String file_url) { pDialog.dismiss(); try { if (Json.has(KEY_SUCCESS)) { String success = Json.getString(KEY_SUCCESS); if (success.equals("1")) { notes = Json.getJsONObject("notes"); for (int i = 0; i < notes.length(); i++) { JsONObject c = notes.getJsONObject(Integer .toString(i)); Log.i("JsONObject c >>", c.toString()); String ID = c.getString(KEY_NOTE_ID); String subject = c.getString(KEY_NOTE_SUBJECT); String date = c.getString(KEY_NOTE_DATE); HashMap<String, String> map = new HashMap<String, String>(); map.put(KEY_NOTE_ID, ID); map.put(KEY_NOTE_SUBJECT, subject); map.put(KEY_NOTE_DATE, date); noteList.add(map); } } } } catch (JsONException e) { e.printstacktrace(); } runOnUiThread(new Runnable() { public voID run() { listadapter adapter = new SimpleAdapter(AllNotes.this, noteList, R.layout.List_item, new String[] { KEY_NOTE_ID, KEY_NOTE_SUBJECT, KEY_NOTE_DATE }, new int[] { R.ID.List_lbl_ID, R.ID.List_lbl_subject, R.ID.List_lbl_date }); setlistadapter(adapter); } }); } }如何在item_List.XML中为TextVIEws设置类型面?我在java代码中为内容视图设置了note_List.所以无法访问List_item.xml的视图.谢谢你的帮助.
解决方法:
您需要覆盖SimpleAdapter的getVIEw(int position,VIEw convertVIEw,VIEwGroup parent)方法并将结果转换为TextVIEw.对于R.layout.List_item布局,这应该是安全的,尽管您可能需要仔细检查.
从那里开始,设置字体照常工作.
片段,放在SimpleAdapter的(匿名)扩展中:
Typeface mTypeface = ... // only needs to be initialised once.@OverrIDe public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) { VIEw vIEw = super.getVIEw(position, convertVIEw, parent); TextVIEw textvIEw = (TextVIEw) vIEw; textvIEw.setTypeface(mTypeface); return textvIEw;}如果,或许在将来的某个时候,你将会有一个更复杂的布局,而不仅仅是一个TextVIEw,我会考虑实现你自己的ArrayAdapter扩展.有很多关于如何去做的例子(也查看VIEwHolder / RowWrapper模式)它会让你完全控制.
编辑:下面的示例代码.
public class TypefacedSimpleAdapter extends SimpleAdapter { private final Typeface mTypeface; public TypefacedSimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); mTypeface = Typeface.createFromAsset(context.getAssets(), /* typeface */); } @OverrIDe public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) { VIEw vIEw = super.getVIEw(position, convertVIEw, parent); TextVIEw textvIEw = (TextVIEw) vIEw; textvIEw.setTypeface(mTypeface); return textvIEw; }}复制粘贴上面的类,并确保根据您的要求设置类型面字段.然后替换当前的SimpleAdapter;即是这样的:
listadapter adapter = new TypefacedSimpleAdapter(AllNotes.this, noteList, R.layout.List_item, new String[] { KEY_NOTE_ID, KEY_NOTE_SUBJECT, KEY_NOTE_DATE }, new int[] { R.ID.List_lbl_ID, R.ID.List_lbl_subject, R.ID.List_lbl_date });请注意,我实际上并没有编译或运行任何此类 *** 作.我会留给你填写任何空白和/或进行更正.
总结以上是内存溢出为你收集整理的android – 为ListView设置TypeFace全部内容,希望文章能够帮你解决android – 为ListView设置TypeFace所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)