如何减小安卓中下拉菜单的高度?

huangapple 未分类评论75阅读模式
英文:

How to reduce the height of Drop Down in android?

问题

我在我的应用程序中使用了默认的安卓下拉框。如何减小下拉高度,因为默认方法不起作用?

英文:

I am using default android spinner in my application. how to reduce the height of drop down as default methods are not working?

答案1

得分: 0

看一下这个答案

Spinner spinner = (Spinner) findViewById(R.id.spinner);
try {
    Field popup = Spinner.class.getDeclaredField("mPopup");
    popup.setAccessible(true);

    // 获取私有的 mPopup 成员变量,并尝试转换为 ListPopupWindow
    android.widget.ListPopupWindow popupWindow = (android.widget.ListPopupWindow) popup.get(spinner);

    // 将 popupWindow 的高度设置为 500 像素
    popupWindow.setHeight(500);
}
catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException | IllegalAccessException e) {
    // 静默处理...
}
英文:

check this 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...
}

答案2

得分: 0

Sure, here's the translated content:

检查一下
<a href="https://stackoverflow.com/a/73580791/13087029">答案</a>

在我的解决方案中,您将回收视图放入微调器中,然后将所有数据放入此回收视图中,您可以修改回收视图的任何内容。

英文:

check this
<a href="https://stackoverflow.com/a/73580791/13087029">answer</a>

In my solution you put recycleview in spinner and then you put all the data to this rv, you can modify everything you want of the recycleview

答案3

得分: -1

使用反射,您可以设置下拉菜单的高度。

Spinner spinner = (Spinner) findViewById(R.id.spinner);
try {
    Field popup = Spinner.class.getDeclaredField("mPopup");
    popup.setAccessible(true);

    // 获取私有 mPopup 成员变量,并尝试强制转换为 ListPopupWindow
    android.widget.ListPopupWindow popupWindow = (android.widget.ListPopupWindow) popup.get(spinner);

    // 将 popupWindow 高度设置为 500 像素
    popupWindow.setHeight(500);
}
catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException | IllegalAccessException e) {
    // 静默处理...
}

如果上述方法不起作用,则可以创建一个类来扩展 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, <状态栏高度>, outRect.right, outRect.bottom);
}
英文:

By Using Refelection you can set height of dropdown

Spinner spinner = (Spinner) findViewById(R.id.spinner);
    try {
        Field popup = Spinner.class.getDeclaredField(&quot;mPopup&quot;);
        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...
    }

If above does not work then Create class extend Spinner class 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.

@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, &lt;STATUS BAR HEIGHT&gt;, outRect.right, outRect.bottom);
}

huangapple
  • 本文由 发表于 2020年8月14日 21:07:52
  • 转载请务必保留本文链接:https://java.coder-hub.com/63413391.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定