将图像添加到Android ListView的简单方法 [英] Easy way to add images to an Android ListView
问题描述
我要添加图像的lisview.我早期发现的示例显示了使用simple_list_item_1的示例,但似乎并不允许我这样做.
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
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();
}
}
这是我的新行布局.我希望这样做,以便在行上的任意位置都可以选择它,而不必点击图像或标签.
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.
推荐答案
对于这种行为,您需要实现自己的适配器.使用Arrayadapter时,应扩展Arrayadapter并覆盖getView方法,以所需的方式更改ListItem的视图.请查看此 ListView教程,以获取有关此主题的更多信息.
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.
这篇关于将图像添加到Android ListView的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!