代码之家  ›  专栏  ›  技术社区  ›  Nuwan Withanage

无法设置API 21和22中Android Spinner弹出窗口的高度

  •  1
  • Nuwan Withanage  · 技术社区  · 7 年前

    在这里,我用了一些风格的“纺纱机”。为了设置微调器弹出窗口的高度,我使用了“列表弹出窗口”。对于更多的Android版本来说,它工作得很好。但不支持API 21和22(Android 5.0和5.1)。下面是我从中找到的代码 How to limit the height of Spinner drop down view in Android

    Spinner newsSourceSpinner = (Spinner)findViewById(R.id.news_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(newsSourceSpinner);
    
        // set popup height
        popupWindow.setHeight(250);
    
    } catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException | IllegalAccessException e) {
        // silently fail...
    }
    

    活动\u main.xml

    <Spinner
           android:id="@+id/news_spinner"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:textSize="13sp"
           style="@style/Widget.AppCompat.Spinner.Underlined"
           android:theme="@style/SpinnerStyle"
           android:popupBackground="#80000000"
           android:focusableInTouchMode="true"
           android:focusable="true"/>
    

    样式.xml

    <style name="SpinnerStyle">
        <item name="colorControlNormal">@color/colorWhite</item>
    </style>
    
    0 回复  |  直到 5 年前