【发布时间】:2014-01-22 18:28:03
【问题描述】:
我真的是 Android 开发的新手,所以如果这个问题对你们中的某些人来说听起来很有趣,我很抱歉。 为了美观,我需要将我的 ListView 菜单的项目居中,但我真的不知道如何,因为代码只用 java 编写,没有使用 XML。
package com.example.mysqltest;
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 {
String classes[] = {"ProductList", "example1", "example2", "example3", "example4", "example5"};
@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) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String local = classes[position];
try {
Class ourClass = Class.forName("com.example.mysqltest." + local);
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
} catch(ClassNotFoundException e){
e.printStackTrace();
}
}
}
【问题讨论】:
-
使用自定义适配器并以您想要的方式自定义布局