Android 自定義 spinner (背景、字型顏色)
Android 自定義 spinner (背景、字型顏色)
轉自:http://blog.sina.com.cn/s/blog_3e333c4a010151cj.html
1、準備兩張圖片,並做好9.png
2、在drawable中定義spinner_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true"
android:drawable="@drawable/spinner_press" /><!--按下時效果-->
<item android:state_pressed="false"
android:drawable="@drawable/spinner" /><!--預設效果-->
</selector>
3、style中定義
<!-- spinner -->
<style name="spinner_style">
<item name="android:background">@drawable/spinner_selector</item>
<item name="android:paddingLeft">5dip</item>
</style>
4、呼叫
<Spinner
android:id="@+id/field_item_spinner_content"
style="@style/spinner_style"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:animationCache="true"
android:drawSelectorOnTop="true" />
5、在layout中定義simple_spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:gravity="center_vertical"
android:textColor="#808080"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
6、java程式碼
ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, R.layout.simple_spinner_item);
String level[] = getResources().getStringArray(R.array.affair_level);//資原始檔
for (int i = 0; i < level.length; i++) {
adapter.add(level[i]);
}
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
效果圖
自定義Spinner五步走http://java-admin.iteye.com/blog/716957
在Android的UI開發中,Spinner(下拉列表)總是可以用到的,一個簡單的自定義Spinner製作我們只需要記住這重要的五步,一個Spinner就可以應用而生了。
(1)新建一個Android工程,名字為SpinnerTest1。修改layout下的main.xml,新增一個Textview和一個Spinner,檔案內容如下:
Xml程式碼
- <? xml version = "1.0" encoding = "utf-8" ?>
- < LinearLayout
- android:id = "@+id/widget28"
- android:layout_width = "fill_parent"
- android:layout_height = "fill_parent"
- android:orientation = "vertical"
- xmlns:android = "http://schemas.android.com/apk/res/android" >
- < TextView
- android:id = "@+id/TextView_Show"
- android:layout_width = "fill_parent"
- android:layout_height = "wrap_content"
- android:text = "你選擇的是"
- android:textSize = "25sp" >
- </ TextView >
- < Spinner
- android:id = "@+id/spinner_City"
- android:layout_width = "fill_parent"
- android:layout_height = "wrap_content" >
- </ Spinner > <!-- 定義一個下拉選單-->
- </ LinearLayout >
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- android:id="@+id/widget28"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- xmlns:android="http://schemas.android.com/apk/res/android" >
- <TextView
- android:id="@+id/TextView_Show"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="你選擇的是"
- android:textSize="25sp">
- </TextView>
- <Spinner
- android:id="@+id/spinner_City"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </Spinner><!-- 定義一個下拉選單-->
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/widget28" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" > <TextView android:id="@+id/TextView_Show" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="你選擇的是" android:textSize="25sp"> </TextView> <Spinner android:id="@+id/spinner_City" android:layout_width="fill_parent" android:layout_height="wrap_content"> </Spinner><!-- 定義一個下拉選單--> </LinearLayout>
(2)修改你的SpinnerTest1類,在這裡我們就要記住五步 來自定義一個Spinner了,完整程式碼及五步註釋如下:
Java程式碼
- package com.xtlh.spinner;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnTouchListener;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Spinner;
- import android.widget.TextView;
- public class SpinnerTest1 extends Activity {
- /** Called when the activity is first created. */
- private List<String> list = new ArrayList<String>();
- private TextView myTextView;
- private Spinner mySpinner;
- private ArrayAdapter<String> adapter;
- private Animation myAnimation;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super .onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //第一步:新增一個下拉列表項的list,這裡新增的項就是下拉列表的選單項
- list.add("北京" );
- list.add("上海" );
- list.add("深圳" );
- list.add("南京" );
- list.add("重慶" );
- myTextView = (TextView)findViewById(R.id.TextView_Show);
- mySpinner = (Spinner)findViewById(R.id.spinner_City);
- //第二步:為下拉列表定義一個介面卡,這裡就用到裡前面定義的list。
- adapter = new ArrayAdapter<String>( this ,android.R.layout.simple_spinner_item, list);
- //第三步:為介面卡設定下拉列表下拉時的選單樣式。
- adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- //第四步:將介面卡新增到下拉列表上
- mySpinner.setAdapter(adapter);
- //第五步:為下拉列表設定各種事件的響應,這個事響應選單被選中
- mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
- public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
- // TODO Auto-generated method stub
- /* 將所選mySpinner 的值帶入myTextView 中*/
- myTextView.setText("您選擇的是:" + adapter.getItem(arg2));
- /* 將mySpinner 顯示*/
- arg0.setVisibility(View.VISIBLE);
- }
- public void onNothingSelected(AdapterView<?> arg0) {
- // TODO Auto-generated method stub
- myTextView.setText("NONE" );
- arg0.setVisibility(View.VISIBLE);
- }
- });
- /*下拉選單彈出的內容選項觸屏事件處理*/
- mySpinner.setOnTouchListener(new Spinner.OnTouchListener(){
- public boolean onTouch(View v, MotionEvent event) {
- // TODO Auto-generated method stub
- /* 將mySpinner 隱藏,不隱藏也可以,看自己愛好*/
- v.setVisibility(View.INVISIBLE);
- return false ;
- }
- });
- /*下拉選單彈出的內容選項焦點改變事件處理*/
- mySpinner.setOnFocusChangeListener(new Spinner.OnFocusChangeListener(){
- public void onFocusChange(View v, boolean hasFocus) {
- // TODO Auto-generated method stub
- v.setVisibility(View.VISIBLE);
- }
- });
- }
- }
記住這五步後,一個Spinner就Ok了,其中在為Spinner的介面卡設定下拉時的選單樣式時,我們可以自定義自己的樣式,如果嫌麻煩,就直接用android.R.layout的,就如下面這樣。
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
效果圖如下:
http://sinye.iteye.com/blog/627023