How to limit the height of Spinner drop down view in Android

后端 未结 6 805
盖世英雄少女心
盖世英雄少女心 2020-11-28 04:34

Please suggest any approach which i use to create it .

Query : I am creating 2-Spinner view , where i have to add Country/Cities li

6条回答
  •  难免孤独
    2020-11-28 05:30

    You can also affect drop down view location and size by subclassing Spinner and overriding its getWindowVisibleDisplayFrame(Rect outRect) which is used by android.widget.PopupWindow for calculations. Just set outRect to limit the area where drop down view can be displayed.

    This approach is of course not suitable for all scenarios since sometimes you want to place drop down view so it doesn't obscure another view or by some other condition known only "outside the instance".

    In my case, I needed to apply FLAG_LAYOUT_NO_LIMITS flag to my activity window which caused the outRect to be huge and therefore part of the drop down view got sometimes hidden behind navigation bar. In order to restore original behavior I used the following override:

    @Override
    public void getWindowVisibleDisplayFrame(Rect outRect) {
        WindowManager wm = (WindowManager) getContext.getSystemService(Context.WINDOW_SERVICE);
        Display d = wm.getDefaultDisplay();
        d.getRectSize(outRect);
        outRect.set(outRect.left, , outRect.right, outRect.bottom);
    }
    

提交回复
热议问题