をドロップダウンリストを開き、知っている私はPopupWindowに深く掘ると、あなたの要件をすることによって実現できることが分かっています
StateListDrawable
以下のコードはeverything-
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と一緒にさまざまなシナリオでそれをテストしました。そして、それは完璧に動作します。
あなたはスピナーでカスタムスタイルを適用する方法を求めていますか?スピナーの上または下にドロップダウンメニューが表示されているかどうかを確認する方法は?あなたのタイトルが混乱しているため明確になる – Anonymous
ドロップダウンメニューがスピナーの上または下に表示されているかどうかを検出する方法は? – Nawaf
あなたのxmlファイルにアンドロイド:spinnerMode = "dialog"を使用してください –