如何在Android中的ListView中放置图像和文本

时间:2012-11-27 00:03:45

标签: android xml image listview

我在这里很新,所以,我有一个问题..我有一个专门的Listview菜单,我想在文本前放一个图像..这是我的menu2.java ..

public class menu2 extends Activity {

private ListView listApps;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu2);

listApps = (ListView) findViewById(R.id.listView1);

String str[] = new String[] {"Messaging", "Phone Dialer", "Simple Math", "Splash", "Exit","Quit Application"};
//final ImageView imageView = (ImageView) findViewById(R.id.);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.activity_list_item,android.R.id.text1, str);

listApps.setAdapter(adapter);

listApps.setOnItemClickListener(new OnItemClickListener(){
    public void onItemClick(AdapterView<?> parent, View aview, int position,
            long id){
        switch(position){
        case 0:
            //imageView.setImageResource(R.drawable.ic_launcher);
            Intent i1 = new Intent(menu2.this,Messaging.class);
            startActivity(i1);
            break;
        case 1:
            Intent i2 = new Intent(menu2.this,PhoneDialer.class);
            startActivity(i2);
            break;                  
        case 2:
            Intent i3 = new Intent(menu2.this,SimpleMath.class);
            startActivity(i3);
            break;                  
        case 3:
            Intent i4 = new Intent(menu2.this,Splash.class);
            startActivity(i4);
            break;                  
        case 4: 
            Intent i5 = new Intent(menu2.this,Exit.class);
            startActivity(i5);
            break;                                      
        case 5:

            AlertDialog.Builder alt_bld = new AlertDialog.Builder(menu2.this);
            alt_bld.setMessage("Are you sure you want to quit application?");
            alt_bld.setCancelable(false);
            alt_bld.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    finish(); 
                }

            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                //  Action for 'NO' Button
                    dialog.cancel();
                }
            });
            AlertDialog alert = alt_bld.create();
            alert.setTitle("Confirmation");
            alert.setIcon(R.drawable.ic_launcher);
            alert.show();                   

        }
    }
});
}

然后我有我的menu2.xml

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

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

</LinearLayout>

我在左侧放置图像视图,在右侧放置文本视图时遇到问题。

2 个答案:

答案 0 :(得分:2)

您是否看到如何使用ListView元素设置布局?您还必须为列表中的每一行创建单独的布局,例如listview_row.xml或您选择命名的任何行。除此之外,您还必须创建一个自定义适配器,它将textView和ImageView绑定到每个ListView行。这使您可以更好地控制列表行。我写了一篇关于这个主题的博客文章。如果您需要更多帮助,请查看并告诉我们。祝你好运!

http://jadebyfield.blogspot.com/2012/10/custom-listview-with-textviews-and.html

答案 1 :(得分:-1)

检查这些链接。您可以使用这些

获得更多使用listview的经验

列出具有相同图标的项目===&gt; Link1

列出具有不同图标的项目===&gt; Link2