listview採用ArrayAdapter時, 如何修改字型,行數等layout
可以採用繼承ArrayAdapter的方式, 自定一個class, 然後改寫getView
如下:
class MyArrayAdapter extends ArrayAdapter { public MyArrayAdapter(Context context, int resource, int textViewResourceId, Object[] objects) { super(context, resource, textViewResourceId, objects); } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = super.getView(position, convertView, parent); TextView textView = (TextView)view.findViewById(android.R.id.text1); //You can increase number of lines in the text view //textView.setSingleLine(true); textView.setMaxLines(2); //textView.setEllipsize(TextUtils.TruncateAt.START); ViewGroup.LayoutParams params = textView.getLayoutParams(); if(params.height > 0) { int height = params.height; params.height = ViewGroup.LayoutParams.WRAP_CONTENT; textView.setLayoutParams(params); //textView.setMinHeight(height); } //Or you can make your font smaller textView.setTextSize(14); //or whatever you like, even apply new custom style //... return view; } }
使用時, 將ArrayAdapter改成MyArrayAdapter
lstview.setAdapter(new MyArrayAdapter(getActivity(), android.R.layout.simple_list_item_multiple_choice, android.R.id.text1, folderItem));另一個參考的例子:Custom ArrayAdapter for a ListView (Android)