Regarding understanding the layout units

雨燕双飞 提交于 2019-12-11 04:27:52

问题


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.

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

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

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"

So my questions are:

  1. Please explain why that is happening?

  2. 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?

code:

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?

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"

you will get the same result.




回答2:


Firstly, you need to read about sizes here. It says

The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.

1-please explain why that is happening?

That's because how those pixels are rendered. When you do it in XML it's statically rendered and when you do it in Java it is dynamically rendered.




回答3:


2-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 of changing the unit of the layoutParameter

ans: The return value will be in px.



来源:https://stackoverflow.com/questions/41074602/regarding-understanding-the-layout-units

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!