Moyen facile d'ajouter des images à un Android ListView
J'ai un lisview que je veux ajouter des images. Les exemples que j'ai trouvé montra très tôt à l'aide de simple_list_item_1, mais il ne semble pas permettre ce que je veux.
Si possible, j'aimerais aussi être en mesure de changer la couleur des objets indepentantly les uns des autres. Ainsi le texte de l'Un rouge, Deux bleus, 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: voici ma nouvelle ligne de mise en page. Je voudrais faire en sorte que les tapant n'importe où sur la ligne pour la sélectionner, au lieu d'avoir à taper l'image ou l'étiquette.
<?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: modification de la disposition linéaire de remplir parent au lieu de renvoyer le texte. Maintenant, il fonctionne.
Créez votre propre
ArrayAdapter
en étendant ArrayAdapter
et surchargeant ses getView(int position, View convertView, ViewGroup parent)
méthode.OriginalL'auteur AndyD273 | 2012-01-26
Vous devez vous connecter pour publier un commentaire.
Pour ce genre de comportement que vous devez mettre en place votre propre adaptateur. Comme vous utilisez un Arrayadapter vous devez étendre Arrayadapter et remplacer la méthode getView pour changer la vue de la ListItem dans la façon dont vous le souhaitez. Jetez un oeil à cette ListView Tutoriel pour plus d'informations sur ce sujet.
OriginalL'auteur Janusz
Dans ce cas, vous devez utiliser CustomAdapters, faire les choses que vous avez besoin de getView() la méthode, consulter le lien ci-dessous pour listview http://www.vogella.de/articles/AndroidListView/article.html
OriginalL'auteur Senthil Mg