关于了解布局单位 [英] Regarding understanding the layout units

查看:66
本文介绍了关于了解布局单位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在计算ListView中某些项目的高度,然后以编程方式将计算出的高度分配给ListView,以确保ListView能够扩展以容纳其内容.我计算了ListView中项目的高度,如下代码所示.

I am calculating the height of some items in a ListView and then I assign the calculated height to the ListView programmatically to ensure that the ListView will expand to accommodate its contents. I calculated the height of the items in the ListView as shown below in the code.

如代码中所示,以下行返回等于533的值:

As shown in the code, the following line returns a value equal to 533:

height += listView.getDividerHeight() * listView.getCount();// 

我遇到的问题是,如代码中所示,当ListView的高度设置为返回的值"533"时,ListView的高度与其内容完全相同.但是当我为ListView设置相同的值"533"时 整个XML文件中的"android:layout_height",则ListView的内容扩展超出其内容高于其内容"

The problem I have is, when the height of the ListView is set to the value returned "533" as shown in the code, the ListView has exactly the same heights as its contents. But when I set the same value "533" to the ListView throughout "android:layout_height" in the XML file, then the ListView expands too much beyond its contents "higher than its contents"

所以我的问题是:

  1. 请解释为什么会发生这种情况?

  1. Please explain why that is happening?

至于已发布的代码,当我将"TypedValue.COMPLEX_UNIT_DIP"更改为任何其他单元(如"TypedValue.COMPLEX_UNIT_PX")时,根本没有任何更改.为什么更改LayoutParameter的单位时没有效果?

As for the posted code, when I change the 'TypedValue.COMPLEX_UNIT_DIP' to any other unit like 'TypedValue.COMPLEX_UNIT_PX' there no change at all. Why there is no effect when changing the unit of the LayoutParameter?

代码:

int height = 0;
for (int i = 0; i < listView.getCount(); i++) {
    View childView = listView.getAdapter().getView(i, null, listView);
    childView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    height+= childView.getMeasuredHeight();
}
//dividers height
height += listView.getDividerHeight() * listView.getCount();// returns 533

param = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, height, this.getResources().getDisplayMetrics()));
param.addRule(RelativeLayout.BELOW, ruleSubject);
container.setLayoutParams(param);
listView.requestLayout();

推荐答案

1-请解释为什么会发生这种情况?

1-please explain why that is happening?

,您正在使用dp中的值.计算出的值533是533px.因此在布局xml文件中使用

in .xml layout files, You are using the values in dp. And the calculated value 533 is 533px.So in layout xml file use

android:layout_height="533px"

您将得到相同的结果.

这篇关于关于了解布局单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆