我真的是Android開發新手,所以我很抱歉,如果這個問題可能聽起來很有趣的一些你。 爲了唯美的緣故,我需要將我的ListView菜單的項目居中,但我真的不知道如何,因爲代碼只用java編寫而沒有使用XML。Android:在java中的ListView菜單中定位項目?
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();
}
}
}
使用自定義的適配器和自定義佈局你想要的方式 – Raghunandan