Android中如何动态改变ListView的高度?
我需要在我的应用程序中动态更改 ListView 的高度。
有什么办法可以做到这一点吗?
I need to change the height of a ListView dynamically in my app.
Is there any way to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这段代码帮助我实现了动态列表视图高度。
在主 Activity 中使用此实用程序类来更改列表视图高度。
取自此处
This piece of code helped me to achieve dynamic listview height.
In the main Activity use this Utility class to change the listview height.
Taken from here
要动态更改高度和宽度,请尝试此操作,
您也可以使用 LayoutParams.FILL_PARENT 或 LayoutParams.WRAP_CONTENT 而不是高度 &宽度
for change height and width dynamically so, try this
you can also use LayoutParams.FILL_PARENT or LayoutParams.WRAP_CONTENT instead of height & width
您可以使用下面的方法根据您的项目以编程方式设置列表视图的高度:
用于设置列表视图高度的方法:
然后像下面的代码一样设置它:
希望它会对您有所帮助。
You can use below method to set the height of listview programmatically as per your items:
Method used for setting height of listview:
Then set it like below code:
Hope it will help you.
首先要记住的是,在使用 mylistview.getmeasureheight 获取列表视图的高度时,它始终根据适配器初始化期间设置的列表内的文本视图/视图给出高度。所以我们需要如下声明适配器:
现在高度固定为 100。请注意,这是文本的高度,而不是视图本身。因此,在添加填充时请记住这一点。
现在设置适配器:
现在,每次将项目添加到列表后,请按如下方式调用函数:
在同一类中定义函数为:
您还可以通过返回 true/false 来检查高度是否设置正确。这个解决方案对我有用。希望这有帮助!!!!
First thing to keep in mind is that while getting height of listview using mylistview.getmeasureheight, it always gives height according to the textview/View inside the list which was set during initializing of adapter. So we need to declare the adapter as follows:
Now height is fixed to 100. Note that it's the height of text and not the view itself. So keep it in mind when adding padding.
Now set the Adapter:
Now after every time you add an item to your list call a function as follows:
define the function in same class as:
You can also check if height is set or not correctly by returning true/false. This solution worked for me. Hope this Helps!!!!!
您必须创建一个新的控件来扩展列表视图,覆盖一些功能
https://github.com/mzlogin/WrapContentListView/blob/master/app/src/main/java/org/mazhuang/wrapcontentlistview/WrapContentListView.java
You have to create a new control that extends the listview overwhriting some functions
https://github.com/mzlogin/WrapContentListView/blob/master/app/src/main/java/org/mazhuang/wrapcontentlistview/WrapContentListView.java
就我个人而言,我花了很多时间试图找到解决方案,结果我所要做的就是将列表视图的高度设置为wrap_content。
Personaly I spent so much time trying to find a solution, turns out all I had to do is to set my listview's height to wrap_content.
使用自定义列表视图并将高度指定为wrap_content。
use customized listview and give height as wrap_content.