Android - 设置ListView项目的高度
我在应用程序中定义布局时遇到问题。我已经按照 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在 R.layout.notes_rownotes_row.xml 中设置高度
是您的项目布局视图
,因此为此布局设置所需的高度。
此布局在项目的以下适配器中传递
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