Android中listview 大小因分辨率而异

 

 

// 获得手机分辨率
	DisplayMetrics dm = new DisplayMetrics();
	getWindowManager().getDefaultDisplay().getMetrics(dm);
	String s = dm.widthPixels + "*" + dm.heightPixels;

	// listview 因分辨率而异
	if (s.equals("480*800") || s.equals("480*854"))
		FixStyle.setListViewHeightBasedOnChildren(listViewActivity, 32);
	else if (s.equals("320*480")) {
		FixStyle.setListViewHeightBasedOnChildren(listViewActivity, 22);

	}
	else if (s.equals("540*960") || s.equals("720*1280")) {
		Log.e("540*960", "540*960");
		FixStyle.setListViewHeightBasedOnChildren(listViewActivity, 32);

	}


import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

public class FixStyle {
    public static void setListViewHeightBasedOnChildren(ListView listView,int itemHeight) {  
        ListAdapter listAdapter = listView.getAdapter();   
        if (listAdapter == null) {  
            return;  
        }  
  
        int totalHeight = listAdapter.getCount() *itemHeight;
  
        ViewGroup.LayoutParams params = listView.getLayoutParams();  
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));  
        listView.setLayoutParams(params);  
    } 
}

 

你可能感兴趣的:(listview 大小,因分辨率而异,Android中)