如何在 Android 中限制 Spinner 下拉视图的高度 [英] How to limit the height of Spinner drop down view in Android
本文介绍了如何在 Android 中限制 Spinner 下拉视图的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请建议我用来创建它的任何方法.
查询:我正在创建 2-Spinner 视图,我必须在其中添加国家/城市列表,所以就像我选择印度然后我在下拉视图中得到 50 个项目一样,问题这是因为它占用了整个页面的高度.
Query : I am creating 2-Spinner view , where i have to add Country/Cities list , So like if i am selecting india then i am getting 50 items inside the drop down view , problem with this is that it is taking the whole page in height .
我想要的:我想创建一个下拉视图,用户只能在其中看到 10 个项目下拉视图,只要用户滚动下拉视图,就会显示其他项目.
What i want : I want to create a drop down view , where user can see only 10 items in the drop down view , other items will be shown whenever user will scroll the drop down view .
推荐答案
您可以使用反射.
Spinner spinner = (Spinner) findViewById(R.id.spinner);
try {
Field popup = Spinner.class.getDeclaredField("mPopup");
popup.setAccessible(true);
// Get private mPopup member variable and try cast to ListPopupWindow
android.widget.ListPopupWindow popupWindow = (android.widget.ListPopupWindow) popup.get(spinner);
// Set popupWindow height to 500px
popupWindow.setHeight(500);
}
catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException | IllegalAccessException e) {
// silently fail...
}
这篇关于如何在 Android 中限制 Spinner 下拉视图的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文