创建选项带图片的菜单

时间:2014-08-17 17:10:04

标签: android-layout android-activity android-listview android-arrayadapter

我试图让我的应用程序更加丰富多彩,并将图片添加到菜单中。菜单显示在启动画面后,工作正常,但我无法添加图片。我只想在每行文字旁边放一些图片。我尝试过的涉及listview教程的所有内容都必须添加更多的类和.xml文件,但仍然没有用。这是使用以下菜单的Menu.java类:

package com.interviewme;
/**
* This class is the menu page to redirect the user to any page they want
*/


import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Menu extends ListActivity {


//names of classes to appear in the menu page
String classes []= {"Play", "About", "InterviewTips",
"Preparing", "Python" , "MySQL"};



@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter <String>(Menu.this, android.R.layout.simple_list_item_1,          classes));

}


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

    super.onListItemClick(l, v, position, id);
    String positionClicked = classes [position];

    //try catch to catch exceptions and create new intent for whatever class is chosen
    try {
        Class ourclass = Class.forName("com.interviewme." +positionClicked);
        Intent ourIntent = new Intent (Menu.this, ourclass);
        startActivity(ourIntent);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

}








Here's the activity_menu.xml code:


<?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"
android:background="#DEED34" >


</LinearLayout>

这里也是我关注但不能开始工作的缩略词:

http://www.vogella.com/tutorials/AndroidListView/article.html

http://www.mkyong.com/android/android-listview-example/

任何建议都会很棒!

1 个答案:

答案 0 :(得分:1)

如果您想在列表视图中包含图片,则必须在布局文件中添加ImageViewTextView,创建一个扩展ArrayAdapter的自定义适配器类,覆盖{该类中的{1}}方法用于设置布局。 然后写 -

getView

而不是 -

setListAdapter(new MyAdapter(this, classes)); 

setListAdapter(new ArrayAdapter <String>(Menu.this, android.R.layout.simple_list_item_1, classes)); 是扩展MyAdapter的类(您必须创建此类)

请参阅您自己的http://mkyong.com链接,并参阅自定义ArrayAdapter示例。