android中控件的测量

xiaoxiao2021-02-28  113

android中控件的测量

在android中自定义view是最常见的运用,测量至关重要,so~ 下面介绍确定dp值得测量和wrap_content/match_parch的不同测量 首先确定dp值的测量:

/**获取listView的高度*/ height = listView.getLayoutParams().height;

但是wrap_content或者march_parent,不能这样获取宽高,必须在measure中传参,重新测量:

if (height == listView.getLayoutParams().MATCH_PARENT || height == listView.getLayoutParams().WRAP_CONTENT){ /**当高度为MATCH_PARENT或者WRAP_CONTENT时 重新获取伸缩图片的高度*/ listView.measure(0,0); int totalHeight = (new ReservationAdapter(mActivity)).getCount() * listView.getMeasuredHeight(); height = totalHeight; } LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) listView.getLayoutParams(); layoutParams.height = height; listView.setLayoutParams(layoutParams);

简单的一个介绍但是有时候确实会犯错误

转载请注明原文地址: https://www.6miu.com/read-44999.html

最新回复(0)