如何检测 android 中的下拉菜单是显示在微调器上方还是下方?
how to detect if the drop down menu is shown above or below the spinner in android?
我正在设计微调器外观定制
我想要的是当微调器弹出窗口下拉到我想使用的微调器下方时
弹出背景图像不同,当它在微调器上方弹出时我想使用不同的弹出背景
这是我的 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"
/>
现在的问题是我怎么知道微调器打开它上方或下方的下拉列表
我深入研究了 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 一起对其进行了测试。而且效果很好。
我正在设计微调器外观定制 我想要的是当微调器弹出窗口下拉到我想使用的微调器下方时 弹出背景图像不同,当它在微调器上方弹出时我想使用不同的弹出背景
这是我的 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"
/>
现在的问题是我怎么知道微调器打开它上方或下方的下拉列表
我深入研究了 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 一起对其进行了测试。而且效果很好。