【安卓】如何製作Spinner的Hint提示效果,並自定義Spinner樣式

這個需求其實還是有的,有時候我們想在Spinner中製作出一個類似於TextView的hint那樣的效果。

如下:


當你第一次看到這個Spinner的時候,其中顯示“點擊選擇”,而你點了一下,就會出現選項,一旦點選了某個選項,“點擊選擇”就會從此消失,也就是說,那只是提示,不參與邏輯判斷。


那麼如何做到這種效果呢?我去stackoverflow上查了一下,發現http://stackoverflow.com/questions/6602339/android-spinner-hint#,這個問題下的回答比較好——

也就是boni2k提供的solution,於是我複製過來做了一些自己的測試和理解如下:


代碼部分:

// 佈局文件 R.layout.spinner_showed_item 決定了spinner中被顯示出的那條的樣式
        // 這個佈局文件有一個默認安卓佈局,名字叫做,自己寫佈局的時候不妨前去參考其中的各種要求
        // 例如必須是一個CheckedTextView 等等,事實上,頂多也就自己改一個背景圖片之類的
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_showed_item) {

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                Log.w(TAG_CRY, "It is getView ing!");

                View v = super.getView(position, convertView, parent);
                //下面這句if似乎註釋掉也不會產生影響
//                if (position == getCount()) {
//                    Log.w(TAG_CRY, "NOW : position == getCount()");
//                    ((CheckedTextView)v.findViewById(R.id.spinner_showed_text)).setText("被顯示的默認文字");
//                    ((CheckedTextView)v.findViewById(R.id.spinner_showed_text)).setHint("被顯示的默認提示"); //"Hint to be displayed"
//                }
                return v;
            }

            @Override
            public int getCount() {
                Log.w(TAG_CRY, "獲得getCount爲:" + super.getCount());
                return super.getCount()-1; // you don't display last item. It is used as hint.
            }

        };

        adapter.setDropDownViewResource(R.layout.spinner_option_items);
        adapter.add(getString(R.string.option_find_subject_math));
        adapter.add(getString(R.string.option_find_subject_chinese));
        adapter.add(getString(R.string.option_click_to_choose));

        Spinner sp = (Spinner) view.findViewById(R.id.sp_Subject);

        sp.setAdapter(adapter);
        sp.setSelection(adapter.getCount()); //display hint


以上中LOG是我的測試,因爲我還並不清楚adapter裏的這幾個被重寫的函數是什麼意思,並且暫時不打算去弄明白(雖然很重要)。注意他那句if,我發現去掉也一樣正常工作,所以並不是很清楚這句的含義,看懂的朋友請在下面評論交流一下。


getCount()-1會使得Spinner一直認爲自己本來就少一條,所以除非你在代碼裏去setSelection(adapter.getCount()),否則這個Spinner永遠不會自動顯示出最後那條。我們就是利用這個原理,將最後一條寫成我們想設的hint,就達到了目的。


我努力試了一下LOG打印出getview 和 getcount的信息,看他們到底什麼時候被調用,不過並沒有發現什麼規律,文檔也看的不是很明白,這個問題放到以後再細究吧。



接下來,我要講一下怎麼設置Spinner的佈局文件,設成自己的樣式,比如你可能想自己修改背景圖片之類的。

上面代碼裏已經提到了

R.layout.spinner_showed_item

R.layout.spinner_option_items

將R.layout.spinner_showed_item背景色設成黃色,xml文件如下:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/spinner_showed_text"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:background="#ff0"
    android:layout_height="match_parent"
    android:ellipsize="marquee"/>

R.layout.spinner_option_items背景色設成藍色,xml文件如下:

<?xml version="1.0" encoding="utf-8"?>

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/spinner_option_text"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:background="#0ff"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:ellipsize="marquee"/>

就會達到如下這種效果,一目瞭然:



我再貼上安卓系統本身自帶的Spinner佈局文件android.R.layout.simple_spinner_dropdown_item,以供我們自己寫的時候參考,注意其中的註釋說明,還是挺有用的。

<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/assets/res/any/layout/simple_spinner_item.xml
**
** Copyright 2008, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License")
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
-->
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/dropdownListPreferredItemHeight"
    android:ellipsize="marquee"/>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章