LoginSignup
12

More than 5 years have passed since last update.

Spinner のドロップダウンメニューだけデザインを変える

Posted at

知らなかったので、メモ。

要約としては、ArrayAdapter#setDropDownViewResource()というのがあり、それにドロップダウン用のレイアウトをあててやるとよい。

SampleActivity.java

//onCreate内
//Spinner
mSpinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter arrayAdapter = ArrayAdapter.createFromResource(this, R.array.option_mode, R.layout.spinner_layout);
//SpinnerAdapterにはsetDropDownViewResourceというメソッドが無いので、ArrayAdapterにする
arrayAdapter.setDropDownViewResource(R.layout.spinner_dropdown_layout);
mSpinner.setAdapter(arrayAdapter);
spinner_layout
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
                 android:id="@+id/text1"
                 android:singleLine="true"
                 android:layout_width="match_parent"
                 android:layout_height="32dp"
                 android:layout_marginLeft="5dp"
                 android:paddingLeft="10dp"
                 android:textColor="@color/text_color_white"
                 android:textSize="@dimen/text_18sp"
                 android:background="@color/main_color"
                 />

spinner_dropdown_layout.xml
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/text1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:textColor="@color/text_color_normal"
          android:padding="10dp"
          android:textSize="@dimen/text_18sp"
          android:background="@color/background_color_white"
    />

こんな具合になります。

スクリーンショット 2015-03-07 23.49.31.png

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12