AppCompat學習(1)-AppCompatSpinner

來源:互聯網
上載者:User

標籤:server   alt   參數   dsp   控制項   ret   檔案夾   res   opd   

andriod中的spinner控制項一共有兩個,一個是本身的Spinner,一個是android.support.v7.widget.AppCompatSpinner。

兩者的區別在於v7內的Spinner是相容低版本的,Spinner再高版本中才能使用的方法換了v7下的Spinner後可以一直相容到2.1 (v7相容到api7),初次之外兩者的使用沒有其他差別,推薦使用v7,保證效果在不同版本上都能顯示。

spinner的使用步驟如下:

 

 

1.設定資料來源

 在values檔案夾下建立如下檔案:

 values/arrays.xml

<!--?xml version="1.0" encoding="utf-8"?--><resources>    <string-array name="week">        <item>星期一</item>        <item>星期二</item>        <item>星期三</item>        <item>星期四</item>        <item>星期五</item>        <item>星期六</item>        <item>星期天</item>    </string-array></resources>

layout/login.xml

<android.support.v7.widget.AppCompatSpinner                android:id="@+id/sp_select_email"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:entries="@array/login_company_email"                android:spinnerMode="dropdown"                android:theme="@style/common_spinner"                />
 android:entries // 傳入的是values檔案夾下的arrayx.xml內的資料android:spinnerMode  //顯示模式有popmenu和dialog兩種android:prompt //當顯示模式為dialog時生效,作用為顯示dialog的標題內容

還有一些其它常用的屬性:

2.設定顯示主題
 <android.support.v7.widget.AppCompatSpinner                android:id="@+id/sp_select_email"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:entries="@array/login_company_email"                android:spinnerMode="dropdown"                android:theme="@style/common_spinner"                />

@style/common_spinner設定的是下拉item的主題。

styles.xml

<style name="common_spinner" parent="Widget.AppCompat.DropDownItem.Spinner">        <item name="android:textColor">@color/app_text_color</item>        <item name="android:textSize">14sp</item>    </style>

 

3.添加響應事件

Spinner的響應事件是OnItemSelectedListener ,千萬不要寫出onItemClickListener,否則直接報錯:

mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {            @Override            public void onItemSelected(AdapterView<!--?--> parent, View view, int position, long id) {                //挑選清單項的操作            }             @Override            public void onNothingSelected(AdapterView<!--?--> parent) {                //未選中時候的操作            }        });

 

也可以使用代碼添加資料來源給spinner,此時布局檔案中只需要包含基本的寬高屬性即可。

適配器的選擇:

最方便的的適配器是ArrayAdapter,缺點是只能顯示單個TextView內容(採用List儲存資料)

ArrayAdapter arrayAdapter = new ArrayAdapter(mContext, R.layout.item_select, mList); //傳入的參數分別為 Context , 未選中項的textview , 資料來源List//單獨設定下拉的textviewarrayAdapter.setDropDownViewResource(R.layout.item_drop);

R.layou.item_select

<!--?xml version="1.0" encoding="utf-8"?--><textview xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width
="match_parent"
  android:textsize
="20dp"
  android:typeface
="serif"
  android:gravity
="center"
  android:textcolor
="#000000"
  android:layout_height
="match_parent"
  android:background
="@color/colorAccent"
  android:orientation
="vertical"> </textview>

R.layout.item_drop

<!--?xml version="1.0" encoding="utf-8"?--><textview xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width
="match_parent"
  android:layout_height
="match_parent"
  android:padding
="5dp"
  android:background
="@color/colorPrimary"
  android:gravity
="center"
  android:orientation
="vertical"
  android:textcolor
="#000000"
  android:textsize
="20dp"> </textview>

效果如所示:

 

有時實際的需求中有可能需要Spinner的下拉不單單顯示一個TextView,那麼這個時候就需要對適配器進行自訂

自訂Adapter:

private class MyAdapter implements SpinnerAdapter {        private ThemedSpinnerAdapter.Helper helper;          @Override        public void registerDataSetObserver(DataSetObserver observer) {         }         @Override        public void unregisterDataSetObserver(DataSetObserver observer) {         }         @Override        public int getCount() {            return mList.size();        }         @Override        public Object getItem(int position) {            return mList.get(position);        }         @Override        public long getItemId(int position) {            return position;        }         @Override        public boolean hasStableIds() {            return false;        }         @Override        public View getView(int position, View convertView, ViewGroup parent) {            TextView textView = new TextView(mContext);            textView.setTextSize(30);            textView.setBackgroundColor(getResources().getColor(R.color.colorAccent));            textView.setText(mList.get(position));            return textView;        }         @Override        public int getItemViewType(int position) {            return 1;        }         @Override        public int getViewTypeCount() {            return 1;        }         @Override        public boolean isEmpty() {            return false;        }         @Override        public View getDropDownView(int position, View convertView, ViewGroup parent) {            TextView textView = new TextView(mContext);            textView.setTextSize(30);            textView.setBackgroundColor(getResources().getColor(R.color.colorPrimary));            textView.setText(mList.get(position));            return textView;        }    }

效果如下(對下拉和預設顯示設定了顏色作為區分,且為方便起見沒用引用布局檔案,設定了下拉位移):

至此適配器的使用的完成了,可以實現預設顯示的和下拉的使用相同布局,也可是不同的布局,基本上滿足需求了

另外還有幾個在23中新出的方法,可以做更多的自訂(使用AppcomptSpinner可適配到低版本):

mSpinner.setPopupBackgroundResource(R.drawable.back);mSpinner.setBackgroundResource(R.drawable.back);
//給預設顯示項目和下拉式功能表設定背景  

 

 

AppCompat學習(1)-AppCompatSpinner

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.