我是 android 新手,我想使用我的自定义布局文件而不是 simple_list_item_1,但它不允许这样做。我想为列表中的每个项目添加一个背景图像(在 layout->custom_view.xml 中定义)或帮助我为整个页面设置背景,在下面的代码中我应该在哪里使用 setContentView 方法。如果要在下面的 .xml 文件中进行任何更改,请告诉我。
public class Planet extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
//setContentView(R.layout.custom_view); Tried to set the view from here but the application is crashing...
String[] planet_names = new String[] {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Sun"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, planet_names);
setListAdapter(adapter);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position == 0)
{
Intent myIntent = new Intent(Planet.this, Galaxy.class);
startActivityForResult(myIntent, 0);
}
}
});
}
custom_view.xml 文件如下,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>