如何解决如何在android中降低Drop Down的高度?
我在我的应用程序中使用默认的android微调器。如何减少默认方法不起作用时下拉菜单的高度?
解决方法
通过反射,您可以设置下拉菜单的高度
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...
}
如果上述方法不起作用,则Create类扩展Spinner
类将覆盖getWindowVisibleDisplayFrame(Rect outRect)
用于计算的android.widget.PopupWindow
。只需将outRect设置为可显示下拉视图的区域即可。
@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,<STATUS BAR HEIGHT>,outRect.right,outRect.bottom);
}
,
选中此answer
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...
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。