在这里,我使用'spinner'和'android:theme'来改变下划线的颜色和下拉旋转箭头的箭头。还有'style'用于获取微调器的基本样式,'android:popupBackground'用于微调器弹出背景颜色。最后我使用'spinner_item.xml'来改变微调器的文本颜色。完成上述所有操作后,微调器不会向上或向下滚动。以下是我的代码。
@风格/ styles.xml
<style name="SpinnerStyle">
<item name="colorControlNormal">@color/colorWhite</item>
</style>
activity.xml
<Spinner
android:id="@+id/news_source_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13sp"
style="@style/Widget.AppCompat.Spinner.Underlined"
android:theme="@style/SpinnerStyle"
android:popupBackground="#80000000"
android:focusableInTouchMode="true"
android:focusable="true"/>
@布局/ spinner_item.xml
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="13sp"
android:gravity="left"
android:padding="5dip"
android:textColor="@color/colorWhite"/>
MainActivity.java
@BindView(R.id.news_source_spinner)
Spinner newsSourceSpinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
spinnerAdapter = new ArrayAdapter<String>(context,R.layout.spinner_item, itemList);
newsSourceSpinner.setAdapter(spinnerAdapter);
}
答案 0 :(得分:1)
最后,我通过以下代码解决了我的问题,为微调器弹出窗口设置固定高度。
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...
}