简体   繁体   中英

Easy way to add images to an Android ListView

I have a lisview that I want to add images to. The examples that I found early on showed using simple_list_item_1, but it doesn't seem to allow what I want.

If possible, I would also like to be able to change the color of items indepentantly of each other. So the text of One would be red, Two blue, etc.

main.xml:

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

    <TextView android:id="@+id/textHeader"
         android:textSize="24px"
         android:textColor="#006699"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content" 
         android:gravity="center_horizontal"
         />
    <ListView  
         android:id="@android:id/list"
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content"
         />
     <TextView android:id="@android:id/empty"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Empty set"
         />
<TextView
    android:id="@+id/status_text"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    />
</LinearLayout>

main.java

public class sGuide extends ListActivity {
    private String[] mBooks = new String[]{ "One", "Two", "Three" };


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ArrayAdapter<String> booksAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mBooks);

        this.setListAdapter(booksAdapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //mChecker.onDestroy();
    }
}

Edit: here is my new row layout. I would like to make it so that tapping anywhere on the row selects it, instead of having to tap the image or label.

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

    <ImageView
        android:id="@+id/icon"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="4dp"
        android:src="@drawable/icon"
         >
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="11dp"
        android:layout_marginBottom="9dp"
        android:text="@+id/label"
        android:textSize="22dp" >
    </TextView>

</LinearLayout>

Edit 2: Changed the linear layout to fill parent instead of wrap text. Now it works.

For this kind of behaviour you need to implement your own adapter. As you are using an Arrayadapter you should extend Arrayadapter and overwrite the getView method to change the view of the ListItem in the way you want it to. Have a look at this ListView Tutorial for more information on this topic.

在这种情况下,您需要使用CustomAdapters,在getView()方法中执行您需要的东西,请参阅下面的链接以获取listview http://www.vogella.de/articles/AndroidListView/article.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM