Android 自訂Spinner和其下拉視窗

來源:互聯網
上載者:User

標籤:des   android   style   blog   class   code   

:

自訂Spinner其實包括兩個部分:

第一部分是用來開啟下拉式清單的按鈕,,這個綠色背景直接設定Spinner的背景就行,素材檔案如下:

裡面的文字需要注意下,Spinner控制項沒有直接修改文字的介面,這個文字實際上是在Adapter中設定,例如:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),

R.layout.spinner_checked_text, gradeList);

gradeSpinner.setAdapter(adapter);

 

這個spinner_checked_text.xml的實現如下:

<?xml version="1.0" encoding="utf-8"?><CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@android:id/text1"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:gravity="center_vertical"    android:padding="10dp"    android:singleLine="true"    android:textColor="@color/text_green"    android:textSize="20sp" ></CheckedTextView>

必須是CheckedTextView,否則會出錯。

 

第二部分就是那個彈出來的下拉視窗,這個是複寫ArrayAdapter的getDropDownView()方法來實現的,代碼如下:

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),                R.layout.spinner_checked_text, gradeList) {            @Override            public View getDropDownView(int position, View convertView,                    ViewGroup parent) {                View view = inflate(getContext(), R.layout.spinner_item_layout,                        null);                TextView label = (TextView) view                        .findViewById(R.id.spinner_item_label);                ImageView check = (ImageView) view                        .findViewById(R.id.spinner_item_checked_image);                label.setText(gradeList.get(position));                if (gradeSpinner.getSelectedItemPosition() == position) {                    view.setBackgroundColor(getResources().getColor(                            R.color.spinner_green));                    check.setImageResource(R.drawable.check_selected);                } else {                    view.setBackgroundColor(getResources().getColor(                            R.color.spinner_light_green));                    check.setImageResource(R.drawable.check_unselect);                }                return view;            }        };        adapter.setDropDownViewResource(R.layout.spinner_item_layout);

其中spinner_item_layout.xml的內容如下:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@color/spinner_light_green"    android:padding="15dp" >    <TextView        android:id="@+id/spinner_item_label"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_centerVertical="true"        android:textColor="@color/text_green"        android:textSize="17sp" />    <ImageView        android:id="@+id/spinner_item_checked_image"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_centerVertical="true"        android:src="@drawable/check_unselect" /></RelativeLayout>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.