在活动中嵌入一个大小的ListView - android

2024-05-23

我正在看这个教程:http://developer.android.com/resources/tutorials/views/hello-listview.html 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 文件中。任何帮助或建议表示赞赏。谢谢。


是的,这是可以做到的。我已经这样做了几次,我的方法是通过扩展 android.widget.BaseAdapter 为列表创建一个自定义适配器。这可能超出了您解决问题所需的范围,但希望这能为您提供所需的内容。

这是主要的 Activity 布局:

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

以下是主活动(Category Activity)如何设置其 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
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在活动中嵌入一个大小的ListView - android 的相关文章

随机推荐

  • 如何在“order by”中添加条件?

    我有一个带有输入参数的存储过程 现在根据这个参数 我的 order by 语句将发生变化 如果输入参数是 ID int类型列 则按ID排序 如果是 ProductType 则按产品类型排序 如果是 IssueDate 则应按问题日期排序 现
  • 迁移问题:MS SQL > MySQL:插入缓冲区内存

    我在使用 MySQL Workbench 上的内置迁移工具时遇到问题 我正在将一个非常大的数据库从 MS SQL 2014 迁移到 MySQL MS SQL 服务器本地部署在我的 Windows 8 1 桌面上 MySQL 服务器在我的网络
  • 用 PHP 截断文件末尾

    我有一个日志文件 我想在 PHP 读取该文件后将其截断 我的代码目前如下所示 fp fopen file r ftruncate fp 125000 fclose fp 但是 这会通过保留first1MB 不过 我想保留last1Mb 的文
  • .NET 正则表达式可匹配任何语言的任何类型的字母

    我可以使用哪种正则表达式来匹配 允许 任何语言的任何类型的字母 我需要匹配任何字母 包括任何变音符号 例如 并排除任何类型的符号 数学符号 货币符号 装饰符号 方框图字符等 和标点符号 我正在使用 ASP NET MVC 2 和 NET 4
  • 融合元组以查找等价类

    假设我们有一个包含 k 个元素的有限域 D d1 dk 我们认为 S 是 D n 的子集 即一组 形式的元组 其中 ai 在 D 中 我们希望使用 S 2 D n 的子集 即一组 形式的元组 其中 Ai 是 D 的子集 来 紧凑地 表示它
  • 仅禁用 resharper 8 (c#) 中的开关片段

    我发现默认的 VS 2013 开关片段比 resharper 的开关片段工作得更好 有什么办法可以禁用它吗 我看到有几种方法可以得到这个 使用 ReSharper IntelliSense 取消选中此处的 开关 ReSharper 模板资源
  • 在设定的时间后从 DOM 中删除元素

    我正在尝试找出在事件触发后从 DOM 中删除元素的 React 方法 我正在尝试发出警报 copySuccess when onClick this props handleCopyFact 被触发 然后在 5 秒后淡出该警报 每个组件的状
  • 为什么 Laravel 中的 .env 文件配置不起作用

    DB CONNECTION mysql DB HOST 127 0 0 1 DB PORT 3306 DB DATABASE DB USERNAME root DB PASSWORD 这是我的 laravel 5 4 配置 但 php ar
  • 如何使用 jQuery 获取 img url?

    是否有可能获得实际的 URL 而不是src使用 jQuery 或 JavaScript 获取当前 DOM 中图像的属性值 即检索 example com foo jpg 而不是 foo jpg 采用因素考虑 还有其他有趣的属性吗 例如 mi
  • Julia Threads.@threads 比单线程性能慢

    我正在尝试求解一维热方程的数值 我正在使用有限差分 并且在 Julia 中使用 threads 指令时遇到一些问题 特别是下面有相同代码的两个版本 第一个是单线程 而另一个使用 threads 除了 thread指令之外 它们是相同的 fu
  • SKScene和SKView的paused属性之间的区别

    我使用以下代码暂停 SKScene self paused YES 然而 根据这个answer https stackoverflow com a 21593852 2043580 by 安德烈 戈尔杰耶夫 https stackoverf
  • 如何将整个 POST 数据转储到 ASP.NET 中的文件中

    我目前正在尝试将一个应用程序从 asp net 移植到 php 但是我遇到了困难 需要有人帮忙 我需要将通过 POST 收到的 aspx 中的所有数据转储到文件中 但我不知道如何执行此操作 有任何想法吗 您可以使用Request 对象的In
  • azure函数或azure逻辑应用程序中是否有Azure文件共享的触发器?

    我在 azure 存储帐户中创建了文件共享 然后使用 Windows 电脑安装了文件共享 接下来 我将文件上传到文件共享驱动器 例如 Z 但每当我将文件上传到 OnPremise 文件共享驱动器时 我想自动触发逻辑应用程序或 azure 功
  • 在反应本机中共享一行的两个按钮

    我有两个看起来像这样的按钮 这是代码 render gt
  • 如何选择列值不不同的每一行

    我需要运行一个 select 语句 返回列值不不同的所有行 例如 EmailAddress 例如 如果表格如下所示 CustomerName EmailAddress Aaron email protected cdn cgi l emai
  • 如何使用 setState 插入 React 的状态数组?

    我正在寻找在反应中修改和数组并在特定索引上插入元素 这就是我的状态 this state arr 我想做的是编译这个arr index random element 反应 js setState 语法 我试图做的是 this setStat
  • NodeJs/WS:如何抛出服务器端在客户端处理的错误?

    当我的 websocket 有超过 2 个连接时 我试图在服务器端抛出错误 我有这个不错的客户端onerror方法 但我无法到达我的代码的那部分 我正在使用 nodeJS 和包ws其中有关于错误处理的最小文档 服务器 js theWebSo
  • Mcrt1.o和Scrt1.o有什么用?

    我坚持使用以下两个文件 即 Mcrt1 o 和 Scrt1 o 谁能帮我知道这两个文件的用途 如何使用它 我们以 gcrt1 o 为例 在使用 pg 选项编译进行性能测试时非常有用 谢谢 表格的文件 crt o总是 C 运行时启动代码 大部
  • 如何提高洪水填充例程的性能?

    我正在我的应用程序中实现四路洪水填充 伪代码如下 Flood fill node target color replacement color 1 If the color of node is not equal to target co
  • 在活动中嵌入一个大小的ListView - android

    我正在看这个教程 http developer android com resources tutorials views hello listview html http developer android com resources t