Android - 设置ListView项目的高度

发布于 2024-12-09 12:50:04 字数 1683 浏览 0 评论 0原文

我在应用程序中定义布局时遇到问题。我已经按照 Android 开发网站上的“记事本教程”进行操作,一切都工作得很好,但唯一我无法实现的是设置 ListView Item 的高度。 项目的宽度和高度取决于其内容。

这是我的布局的 XML 定义:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="fill_parent" android:layout_width="match_parent" android:orientation="vertical">
    <ListView android:layout_width="fill_parent" android:id="@+id/android:list" android:transcriptMode="alwaysScroll" android:layout_height="match_parent">
    </ListView>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/text1"
      android:layout_width="fill_parent"
      android:layout_height="80dp"  
      android:padding="10dp"
      android:textSize="16sp"/>

</LinearLayout>

在我的代码中,我仅在 oncreate() 方法中调用整个 ListView,然后在该方法中调用,该方法负责使用 SQLLite 中的数据填充列表项

 private void fillData() {
    // Get all of the rows from the database and create the item list
    Cursor mNotesCursor = mDbHelper.fetchAllNotes();
    startManagingCursor(mNotesCursor);

    // Create an array to specify the fields we want to display in the list (only TITLE)
    String[] from = new String[]{NotesDbAdapter.KEY_TITLE};

    // and an array of the fields we want to bind those fields to (in this case just text1)
    int[] to = new int[]{R.id.text1};

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter notes = 
            new SimpleCursorAdapter(this, R.layout.notes_row, mNotesCursor, from, to);
    setListAdapter(notes);

}

即使当我尝试在我的列表中设置一些疯狂的值时XML 作为 TextView 属性,项目高度,文本颜色,一切都保持不变。 我想它应该很简单,但不幸的是我无法做到这一点

I have problem with defining layout in my application. I have followed "notepad tutorial" on Android Development site, all is working excellent, but only thing I am not able to achieve is set ListView Item's height.
Items are only as wide and high as its content is.

Here is my XML definition of the layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="fill_parent" android:layout_width="match_parent" android:orientation="vertical">
    <ListView android:layout_width="fill_parent" android:id="@+id/android:list" android:transcriptMode="alwaysScroll" android:layout_height="match_parent">
    </ListView>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/text1"
      android:layout_width="fill_parent"
      android:layout_height="80dp"  
      android:padding="10dp"
      android:textSize="16sp"/>

</LinearLayout>

In my code I am calling only in oncreate() method the entire ListView and then in this method, which is responsible for fill listitems with data from SQLLite

 private void fillData() {
    // Get all of the rows from the database and create the item list
    Cursor mNotesCursor = mDbHelper.fetchAllNotes();
    startManagingCursor(mNotesCursor);

    // Create an array to specify the fields we want to display in the list (only TITLE)
    String[] from = new String[]{NotesDbAdapter.KEY_TITLE};

    // and an array of the fields we want to bind those fields to (in this case just text1)
    int[] to = new int[]{R.id.text1};

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter notes = 
            new SimpleCursorAdapter(this, R.layout.notes_row, mNotesCursor, from, to);
    setListAdapter(notes);

}

Even when I tried set some crazy values on my XML as TextView attributes, items height, textcolor, everything remained unchanged.
I suppose its should be simple, but unfortunately I am not able to make it w

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

も星光 2024-12-16 12:50:04

尝试在 R.layout.notes_rownotes_row.xml 中设置高度

是您的项目布局视图

,因此为此布局设置所需的高度。

此布局在项目的以下适配器中传递

SimpleCursorAdapter notes = 
            new SimpleCursorAdapter(this, R.layout.notes_row, mNotesCursor, from, to);

try to set the height in R.layout.notes_row

notes_row.xml is your item layout view

and so set required height for this layout.

this layout is passed in following adapter for the item

SimpleCursorAdapter notes = 
            new SimpleCursorAdapter(this, R.layout.notes_row, mNotesCursor, from, to);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文