android 列表视图项目高度
为什么当我将 SimpleCursorAdapter 用于 ListView 时,我在 ListView 中的项目高度如下 -
(我的代码基于 这个)
但是使用数组时Listview项目的高度很大
< img src="https://i.sstatic.net/tSrYu.png" alt="listview big">
(我基于 这个)
项目列表视图的行布局是
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/text1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
所以我的问题是为什么使用ArrayAdapter和SimpleCursorAdapter时行高存在差异?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
似乎没有效果。
为我改变了高度
seemed no effect.
changed the height for me
对我来说,诀窍不是设置高度,而是设置 minHeight。这必须应用于自定义适配器用于呈现每一行的任何布局的根视图。
The trick for me was not setting the height -- but instead setting the minHeight. This must be applied to the root view of whatever layout your custom adapter is using to render each row.
您需要在列表项布局上使用填充,以便在项目边缘添加空间(仅增加字体大小并不能做到这一点)。
You need to use padding on the list item layout so space is added on the edges of the item (just increasing the font size won't do that).
我做了类似的事情:
您必须将两个字段都放入 textView.setHeight(30);
textView.setMinimumHeight(30);否则它不会改变任何事情。对我来说它有效&我有同样的问题。
I did something like that :
You must put the both fields textView.setHeight(30);
textView.setMinimumHeight(30); or it won't change anything. For me it worked & i had the same problem.
列表视图项目的高度根据其内容进行调整。第一张图片中没有内容。所以高度非常低。在第二张图片中,高度根据文本的大小而增加。因为,您指定了 android:layout_height="wrap_content"。
The height of list view items are adjusted based on its contents. In first image, no content. so height is very minimum. In second image, height is increased based on the size of the text. Because, you specified android:layout_height="wrap_content".
这是我的解决方案;
这是我在 BaseAdapter 子类中的 getView() :
这里我将 ListItem 的最小高度设置为 100;
Here is my solutions;
It is my getView() in BaseAdapter subclass:
Here i have set ListItem's minimum height to 100;
这是我的解决方案(有一个嵌套的 LinearLayout):
This is my solution(There is a nested LinearLayout):