简体   繁体   中英

Android studio get screen height in class

Is it possible to get my screen height in this function? i want to make my listview 80% of my screen size but am unable to figure this out.

private void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        int len = listAdapter.getCount();
        for (int i = 0; i < len; i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }



        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = 2000; //+ (listView.getDividerHeight() * (listAdapter.getCount() - 1);
        listView.setLayoutParams(params);
    }```

You can find the dimensions of your device screen with the following:

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;

我的答案可能不正确,但是在使用屏幕分辨率时,我使用了以下代码:

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM