Java 无法设置API 21和22中Android Spinner弹出窗口的高度
在此,我使用了一些风格的“旋转器”。为了设置微调器弹出窗口的高度,我使用了“列表弹出窗口”。对于更多的Android版本来说,它工作得很好。但不支持API 21和22(Android 5.0和5.1)。下面是我从中找到的代码 MainActivity.javaJava 无法设置API 21和22中Android Spinner弹出窗口的高度,java,android,android-widget,field,android-spinner,Java,Android,Android Widget,Field,Android Spinner,在此,我使用了一些风格的“旋转器”。为了设置微调器弹出窗口的高度,我使用了“列表弹出窗口”。对于更多的Android版本来说,它工作得很好。但不支持API 21和22(Android 5.0和5.1)。下面是我从中找到的代码 MainActivity.java Spinner newsSourceSpinner = (Spinner)findViewById(R.id.news_spinner); try { Field popup = Spinner.class.getDeclare
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"/>
<style name="SpinnerStyle">
<item name="colorControlNormal">@color/colorWhite</item>
</style>
styles.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"/>
<style name="SpinnerStyle">
<item name="colorControlNormal">@color/colorWhite</item>
</style>
@彩色/彩色白色