如何检测如果下拉菜单显示在android的微调上方或下方?

我在devise工作是微调看起来自定义我想要的是当微调popup下拉下面的微调我想使用popupbackground图像不同,当它popup上方微调我想要使用popup窗口背景

这里是我的Xml代码:

<Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" android:spinnerMode="dropdown" android:popupBackground="@drawable/spinnerbottombg" android:overlapAnchor="false" android:drawSelectorOnTop="false" /> 

现在的问题是我如何知道微调打开下拉列表上方或下方

Solutions Collecting From Web of "如何检测如果下拉菜单显示在android的微调上方或下方?"

我深入挖掘PopupWindow,发现你的需求可以通过

StateListDrawable

以下代码将解释一切 –

activity_main.xml中

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.mmbarno.dummyalertdialog.MainActivity"> <Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:spinnerMode="dropdown" android:overlapAnchor="false" android:drawSelectorOnTop="false" /> </RelativeLayout> 

popup_bg_above.xml

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#FF4081" /> </shape> 

popup_bg_below.xml

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#3F51B5" /> </shape> 

最后在MainActivity.java中

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Spinner spinner = (Spinner) findViewById(R.id.spinner); spinner.setPopupBackgroundDrawable(getPopupBg()); populateSpinner(); } private void populateSpinner() { ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); } private StateListDrawable getPopupBg() { StateListDrawable states = new StateListDrawable(); int identifier = Resources.getSystem().getIdentifier("state_above_anchor", "attr", "android"); states.addState(new int[] {identifier}, ContextCompat.getDrawable(this, R.drawable.popup_bg_below)); states.addState(new int[]{}, ContextCompat.getDrawable(this, R.drawable.popup_bg_above)); return states; } 

希望你的要求将得到满足。 我已经与ScrollView一起在不同的场景中testing过它。 它完美的作品。

弹出下面

弹出式上面