我正在看这个教程:http://developer.android.com/resources/tutorials/views/hello-listview.html
是否可以将列表嵌入我当前的活动中,以便我可以设置其高度。我的布局如下:
<?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="@drawable/mobile_vforum_bg"
>
<ImageView
android:src="@drawable/mobile_vforum"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/logo"
android:layout_marginLeft="57px"
android:layout_marginTop="10px">
</ImageView>
<TextView
android:layout_height="30px"
android:layout_width="fill_parent"
android:id="@+id/welcomeText"
android:textSize="16px"
android:textColor="#317c73"
android:background="#eeeeee"
android:shadowColor="#333333"
android:shadowDx="1"
android:shadowDy="1"
android:paddingTop="4px"
android:paddingLeft="7px">
</TextView>
<ListView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/projectList">
</ListView>
</LinearLayout>
您可以在布局结束时看到我的ListView。基本上我有一个图像和文本行,我希望列表位于其下方。我发布的教程链接扩展了ListActivity,这是我感到困惑的地方,因为我在扩展Activity的当前布局java文件中。任何帮助或建议表示赞赏。感谢。
答案 0 :(得分:3)
是的,可以这样做。我已经完成了几次,我的方法是通过扩展android.widget.BaseAdapter为列表创建一个自定义适配器。这可能比解决问题所需要的更多,但希望这可以为您提供所需的信息。
这是主要的活动布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="6dip"
android:background="@drawable/backrepeat"
>
<Button
android:id="@+id/cat_done_button"
android:layout_width="75dip"
android:layout_height="50dip"
android:layout_marginTop="10dip"
android:layout_marginLeft="6dip"
android:text="@string/done"
/>
<EditText
android:id="@+id/category_entry"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:hint="@string/category_hint"
android:layout_margin="10dip"
android:layout_below="@+id/cat_done_button"
/>
<ImageButton
android:id="@+id/category_add_button"
android:background="#00000000"
android:src="@drawable/ic_menu_add_ld"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_marginLeft="5dip"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:layout_alignBottom="@+id/category_entry"
android:layout_toRightOf="@+id/category_entry"
/>
<ListView
android:id="@+id/category_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginBottom="5dip"
android:background="#00000000"
android:layout_below="@+id/category_notification"
/>
</RelativeLayout>
以下是列表中每行的单独布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="6dip"
android:background="#00000000"
>
<TextView
android:id="@+id/category_name"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginTop="10dip"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:layout_alignParentLeft="true"
android:background="#00000000"
/>
<ImageButton
android:id="@+id/category_delete_button"
android:background="#00000000"
android:src="@drawable/ic_menu_delete_ld"
android:layout_width="50dip"
android:layout_height="50dip"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:layout_alignParentRight="true"
android:focusable="false"
android:focusableInTouchMode="false"
/>
</RelativeLayout>
以下是主要活动(CategoryActivity)如何设置其List对象:
private void setUpCategoryList() {
categoryList = (ListView) this.findViewById(R.id.category_list);
categoryAdapter = new CategoryAdapter(this, android.R.layout.simple_list_item_1,
categoryManager.getCategoriesList());
categoryList.setAdapter(categoryAdapter);
}
这是自定义的CategoryAdapter:
public class CategoryAdapter extends BaseAdapter implements OnClickListener {
private CategoryActivity catActivity;
private List<String> categoryList;
private final String CAT_DELETED = "Category has been deleted.";
private final String DEFAULT_DELETE = "Cannot delete Default category.";
private final String NON_EMPTY_DELETE = "Category has entries. Entries re-assigned to Default category.";
public CategoryAdapter(CategoryActivity catActivity, int resource, List<String> items) {
this.catActivity = catActivity;
categoryList = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String category = categoryList.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) catActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.category_row_layout, null);
}
TextView categoryView = (TextView) convertView.findViewById(R.id.category_name);
categoryView.setText(category);
ImageButton deleteButton = (ImageButton) convertView.findViewById(R.id.category_delete_button);
deleteButton.setOnClickListener(this);
deleteButton.setTag(category);
return convertView;
}
@Override
public void onClick(View view) {
//Do stuff here
}