0

我想在设置适配器之后或之前为列表活动中列表视图上的某些项目自定义一些设置(颜色、边距)。我怎样才能做到这一点?有什么可以覆盖的功能吗?

谢谢你。

4

2 回答 2

3

您可以使用自己的列表适配器.. http://www.vogella.com/articles/AndroidListView/article.html

于 2013-06-11T16:02:29.253 回答
0

这是自定义列表项的方法:

您的列表布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

<ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>

每个项目的布局:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

<TextView
        android:id="@+id/text"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        />

</LinearLayout>

最后你的活动:

public class MyActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_activitty);

    final List<String> list = new ArrayList<String>();
    list.add("test");
    list.add("test");
    list.add("test");

    final CustomAdapter adapter = new CustomAdapter(this, list);
    final ListView listView = getListView();
    listView.setAdapter(adapter);
}


public class CustomAdapter extends BaseAdapter {

    private Context mContext;
    private List<String> mList;

    public CustomAdapter(Context context, List<String> list) {
        mContext = context;
        mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public String getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final Holder holder;

        if (convertView == null) {

            // if it is the first time you create the row
            // you get the layout of each row here
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list, null);

            // you keep your layout in a holder
            holder = new Holder();
            holder.mText = (TextView) convertView.findViewById(R.id.text);
            convertView.setTag(holder);

        } else {

            // if the row has already been created, you get it from the holder
            holder = (Holder) convertView.getTag();
        }

        // you do what you want with the content

        holder.mText.setText(getItem(position));
        holder.mText.setTextColor(Color.BLUE);

        return convertView;
    }

    private class Holder {
        public TextView mText;
    }
}
}
于 2013-06-11T19:34:52.523 回答