Android, custom spinner

이도현·2023년 8월 11일
0

Android 공부

목록 보기
17/30
  • spinner_custom.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/transparent"/>
        </shape>
    </item>
    <item android:gravity="center_vertical|right"
        android:right="10dp">
        <layer-list>
            <item  android:gravity="right" android:width="15dp" android:height="9dp" >
                <bitmap android:src="@drawable/icon_minimenu" android:tint="@color/white"/>
            </item>
        </layer-list>
    </item>
</layer-list>
  • spinner layout
<LinearLayout
                    android:id="@+id/layout_spinner"
                    android:layout_width="250dp"
                    android:layout_height="30dp"
                    android:background="#22436F"
                    android:gravity="center"
                    android:orientation="horizontal">

                    <androidx.appcompat.widget.AppCompatSpinner
                        android:id="@+id/spinner"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:background="@drawable/spinner_custom"
                        android:gravity="center"
                        android:overlapAnchor="false"
                        android:popupElevation="10dp"
                        android:popupBackground="#DEDEDE"
                        android:textColor="@color/white"
                        android:textSize="13sp" />
                </LinearLayout>
  • spinner_dropdown.item.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:background="@drawable/background_spinner_item"
    android:layout_gravity="center"
    android:maxLines="1"
    android:paddingTop="4dp"
    android:paddingBottom="4dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:gravity="center"
    android:textAlignment="center"
    android:textSize="13sp"
    android:textColor="#22436F"/>
  • background_spinner_item
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:top="-6dp"
        android:left="-6dp"
        android:right="-6dp">

        <shape android:shape="rectangle">
            <solid android:color="@color/transparent"/>
            <stroke
                android:width="1dp"
                android:color="@color/white"/>
        </shape>
    </item>
</layer-list>
  • adapter 설정
val itemArray = arrayOf("3", "4", "5", "6", "7", "8", "랜덤")
      val spinnerAdapter = ArrayAdapter(
                this,
                R.layout.spinner_dropdown_item,
                itemArray
            )
            spinner.adapter = spinnerMarginAdapter
            spinner.viewTreeObserver.addOnGlobalLayoutListener {
                (spinner.selectedView as TextView).setTextColor(Color.WHITE)
                (spinner.selectedView as TextView).setBackgroundResource(R.drawable.spinner_custom)
            }
       spinner.onItemSelectedListener =
                object : AdapterView.OnItemSelectedListener {
                    override fun onItemSelected(
                        parent: AdapterView<*>,
                        view: View,
                        position: Int,
                        id: Long
                    ) {
                        // 선택됬을 경우
                     
                    }

                    override fun onNothingSelected(parent: AdapterView<*>) {}
                }
profile
좋은 지식 나누어요

0개의 댓글