- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
{ January 14, 2011... I have given up to use setListViewHeightBasedOnChildren(ListView listView},
instead, I don't put my listview in a scrollview, and then just put other contents
into a listview by using ListView.addHeaderView() and ListView.addFooterView().
http://dewr.egloos.com/5467045 }
ViewGroup(ViewGroup 包含具有除换行符以外的长文本的 TextViews)。getMeasuredHeight 返回错误值...小于实际高度。
如何解决这个问题?
这是Java代码:
/*
I have to set my listview's height by myself. because
if a listview is in a scrollview then that will be
as short as the listview's just one item.
*/
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int count = listAdapter.getCount();
for (int i = 0; i < count; i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(View.MeasureSpec.AT_MOST, View.MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
这是 list_item_comments.xml:
最佳答案
这个问题比较老了,但我遇到了类似的问题,所以我会描述哪里出了问题。其实listItem.measure()中的参数用错了,应该这样设置:
listItem.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED))
但是,要小心未指定的宽度测量规范,它会忽略所有布局参数甚至屏幕尺寸,因此要获得正确的高度,首先要获得 View 可以使用的最大宽度并以这种方式调用 measure():
listItem.measure(MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
关于android - ViewGroup{TextView,...}.getMeasuredHeight 给出的错误值小于实际高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4668939/
我正在尝试确定某些 UI 元素的实际尺寸(以像素为单位)! 这些元素是从 .xml 文件中扩展而来的,并使用倾斜宽度和高度进行初始化,以便 GUI 最终支持多种屏幕尺寸和 dpi (as recomm
我有这样的 XML: 我在方法 onMesure 中获取 View 高度,如下所示: protected void onMeasure(int widthMeasureSpec, int heig
我正在尝试以编程方式获取 ListView 中项目的高度。所以我在我们的网站上提到了一些电子邮寄,我得出了以下代码 private float calcListViewItemsHeight(
{ January 14, 2011... I have given up to use setListViewHeightBasedOnChildren(ListView listView},
我正在经历这个http://developer.android.com/guide/topics/ui/declaring-layout.html当我看到一条线表明高度可以但不一定与测量高度不同时,我
我整个上午都在为 Android 上的这个问题苦苦思索。我有一个 ListView,我需要为其计算高度。 我正在使用这段代码: int totalHeight = 0; for (int size =
我尝试在 onGlobalLayout 中调用 View.getMeasuredHeight 和 View.getHeight。两者都给了我相同的结果。 我想知道,在什么情况下它们的值会有所不同?有没
我正在尝试确定 TextView 在绘制之前 的高度。我正在使用以下代码来做到这一点: TextView textView = (TextView) findViewById(R.id.textvie
我真的对 android 为布局所做的整个测量事情感到困惑。基本上,我想在布局之前获取 View 的实际计算高度和宽度。我需要获得计算出的高度和宽度,因为我有一个隐藏的 LinearLayout,我想
ShadowSpan.java public class ShadowSpan extends ReplacementSpan { public ShadowSpan(int color, Point
我有一个问题。 我希望将行动态添加到 TableLayout。添加这些行时,我需要能够获取 View 的大小,因此我尝试在 onSizeChanged() 期间执行此操作,但新行不显示。所以我尝试了
我在 LinearLayout 中有一个 LinearLayout 并且子 linearlayout 有它的 layout params 其中 height 和 width 定义为 LinearLay
所以我正在搜索抛出所有 getHeight/widht 问题帖子,并尝试不同的方法来解决问题(如 globalLayoutListener 或 Runnable),等待,直到 FrameLayout
我写了一个非常简单的 View 组扩展 LinearLayout 如下。 @Override protected void onMeasure(int widthMeasureSpec, i
在使用 view.measure() 测量具有恒定尺寸 的View 之后,getMeasuredHeight() getMeasureWidth() 返回 0。 layout_view.xml,扩展以
我是一名优秀的程序员,十分优秀!