Android:更改ListView的样式

时间:2014-08-01 19:32:24

标签: android listview android-listview

我制作了一个ListView,它看起来像第一个图像,但我想让它像普通的ListView一样(就像第二个图像一样)。我使用TextView作为项目和一个简单的自定义适配器。

第一

enter image description here

第二

enter image description here

activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:baselineAligned="false">

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

</LinearLayout>

items.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textViewItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Item name here..."/>

自定义适配器

public class TextVAdapter extends ArrayAdapter<CategoryItem> {

    Context mContext;
    int layoutResourceId;
    List<CategoryItem> data = null;

    public TextVAdapter(Context retrieveFeedTask, int layoutResourceId, List<CategoryItem> messages) {

        super(retrieveFeedTask, layoutResourceId, messages);

        this.layoutResourceId = layoutResourceId;
        this.mContext = retrieveFeedTask;
        this.data = messages;
    }

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

        /*
         * The convertView argument is essentially a "ScrapView" as described is Lucas post
         * http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
         * It will have a non-null value when ListView is asking you recycle the row layout.
         * So, when convertView is not null, you should simply update its contents instead of inflating a new row layout.
         */
        if(convertView==null){
            // inflate the layout
            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();                
            convertView = inflater.inflate(layoutResourceId, parent, false);
        }

        // object item based on the position
        CategoryItem objectItem = data.get(position);

        // get the TextView and then set the text (item name) and tag (item ID) values
        TextView tv = (TextView) super.getView(position, convertView, parent);
        tv.setText(objectItem.getTitle());

        return convertView;

    }

}

3 个答案:

答案 0 :(得分:1)

这样做很简单。 因为android系统中有示例列表视图。 你不必使用太多代码。就像这样

public class MainActivity extends ListActivity{
String stringarray[]={"Apple","Grape","Orange"};
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,stringarray));
}


}

答案 1 :(得分:1)

如果您需要简单,请从@OmerFaruk回答,

如果您需要更多地控制代码,请尝试提取列表项的标准布局属性(如填充,textAppearence,背景,重力),例如android.R.layout.simple_list_item_1,android.R.layout.simple_list_item_activated_1 ,android.R.layout.simple_list_item_single_choice或类似的,并将它们插入“items.xml”

答案 2 :(得分:1)

这很简单 - 您可以通过多种方式执行此操作

activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:background="@android:color/black"
android:baselineAligned="false">

     <ListView
          android:id="@+id/list"
          android:background="@android:color/black"
          android:layout_height="wrap_content"
          android:layout_width="match_parent">
     </ListView>

</LinearLayout>

items.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textViewItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textColor="@android:color/white"
android:gravity="center"
android:text="Item name here..."/>

测试并根据需要进行细微更改,如果您需要任何帮助,请与我们联系