在活动中嵌入一个大小的ListView - 机器人 [英] Embedding a sized ListView in activity - android

查看:171
本文介绍了在活动中嵌入一个大小的ListView - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在看这个教程: http://developer.android .COM /资源/教程/视图/ HELLO-listview.html

是否有可能嵌入到我目前的活动列表,以便我可以设置其高度。我的布局是这样的:

Is it possible to embed the list in my current activity so I can set its height. My layout looks like this:

<?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这是我感到困惑,因为我在扩展活动我目前的布局的java文件。任何帮助或建议是AP preciated。谢谢你。

You can see my ListView at the end of the layout. Basically I have an image and line of text and I want the list to reside below that. The tutorial link I posted extends ListActivity which is where I get confused since I am in my current layouts java file that extends Activity. Any help or suggestions is appreciated. Thanks.

推荐答案

是的,这是可以做到这一点。我已经做了几次,我的方法是通过扩展android.widget.BaseAdapter创建列表中的自定义适配器。这可能是比你更需要解决你的问题,但希望这给了你所需要的。

Yes, it is possible to do this. I've done it a few times, and my approach was to create a custom Adapter for the list by extending android.widget.BaseAdapter. This may be more than you need to solve your problem, but hopefully this gives you what you need.

下面是主要的活动布局:

Here is the main Activity layout:

<?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>

下面为列表中的每个行的单独的布局:

Here is the separate layout for each row in the list:

<?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)设置其列出对象:

Here is how the main activity (CategoryActivity) sets up its List object:

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:

Here is the custom 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
}

这篇关于在活动中嵌入一个大小的ListView - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆