我在我的应用程序中使用默认的android微调器。当默认方法不起作用时,如何降低下拉列表的高度?
发布于 2020-08-14 21:17:03
检查此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...
}发布于 2020-08-14 21:15:39
通过使用Refelection,您可以设置下拉菜单的高度
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...
}如果上面的方法不起作用,那么创建类扩展Spinner类,覆盖android.widget.PopupWindow用于计算的getWindowVisibleDisplayFrame(Rect outRect)。只需将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);
}https://stackoverflow.com/questions/63413391
复制相似问题