2014. 1. 15. 16:50 안드로이드

안드로이드 가변적인 LISTVIEW의 높이에 맞게 레이아웃을 잡을려고 할때 쓰나 



private int getListViewHeight(ListView list) {

 ListAdapter adapter = list.getAdapter();

 int listviewHeight = 0;

 list.measure(MeasureSpec.makeMeasureSpec(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

 listviewHeight = list.getMeasuredHeight() * adapter.getCount() + (adapter.getCount() * list.getDividerHeight());

 return listviewHeight;

 }




mListView_ExamUnitList.invalidate();


int listHeight = getListViewHeight(mListView_ExamUnitList);

RelativeLayout.LayoutParams params2 =  new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,listHeight);

params2.addRule(RelativeLayout.BELOW,R.id.usersetting_spinner);

params2.setMargins(Util.dpToPx(getResources(), 10), Util.dpToPx(getResources(), 10), Util.dpToPx(getResources(), 10), 0);

mListView_ExamUnitList.setLayoutParams(params2);






요건 DP를 픽셀로 바꾸는 것

public static int dpToPx(Resources res, int dp) {

return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, res.getDisplayMetrics());

}

posted by 욱이다