woshidan's blog

あいとゆうきとITと、とっておきの話。

Spinnerのレイアウトをコードで変更する

スタイルを利用した指定がうまく動かなかったり見つけるまでにちょっと時間がかかったりしたので、まあ適当にメモ。

  1. android:id="@android:id/text1"@android:id/text1がidに指定されたTextViewだけのレイアウトリソースを用意
  2. Spinnerの要素をセットする ArrayAdapter の引数に渡す
  3. ドロップダウン用のレイアウトは、ArrayAdapter.setDropDownViewResourceメソッドの引数に渡す

Adapterに手動で要素を追加しているのは、利用する時にスピナーの要素が動的に変わっていたからです。

また、Spinnerの要素の先頭に画像などをセットしたい場合は、BaseAdapterを継承してgetViewメソッドをオーバーライドして用います。

setContentView(R.layout.activity_main);

ArrayAdapter<String> adapter = new ArrayAdapter(this, R.layout.item_in_list);
adapter.add("One");
adapter.add("Two");
adapter.add("Three");
adapter.setDropDownViewResource(R.layout.item_in_drop_down_list);
AppCompatSpinner spinner = (AppCompatSpinner) findViewById(R.id.imageFolderNameSpinner);
spinner.setAdapter(adapter);

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
});
<!-- res/layout/activity_main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.woshidan.myapplication.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay">
            <android.support.v7.widget.AppCompatSpinner
                android:id="@+id/imageFolderNameSpinner"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />
        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>
<!-- item_in_list.xml -->
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@android:id/text1"
    android:textSize="18dp"
    android:padding="8dp"
    android:textColor="#0f0"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:background="#fff"
    android:textAlignment="inherit"
    xmlns:android="http://schemas.android.com/apk/res/android" />
<!-- item_in_drop_down_list.xml -->
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@android:id/text1"
    android:textSize="18dp"
    android:padding="8dp"
    android:textColor="#f00"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:textAlignment="inherit"
    xmlns:android="http://schemas.android.com/apk/res/android" />

f:id:woshidan:20160809000455g:plain

スタイルで指定できるはずなんだけどなぁ。