自定义下拉菜单模式Spinner与setDropDownViewResource
Spinner就是下拉菜单,也等于swing的combo box、html的<select>,由于手机画面有限,要在有限的范围选择项目,下拉菜单是唯一、也是较好的选择。
本范例的示范重点在于自定义下拉菜单里的样式,其关键在于调用setDropDownResource方法,以XML的方式定义下拉菜单要显示的模样。本程序还设计了一段动画,当User以触控的方式单击这个自定义的Spinner时,会以一段动画提示User,下面的实现源码:
自定义下啦菜单样子的XML,位于res/layout/myspinner_dropdown.xml:
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="30dp" android:singleLine="true" style="?android:attr/spinnerDropDownItemStyle"> </TextView>
自定义spinner动画,位于res/anim/spinner_anim.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="300"/> <alpha android:fromAlpha="1.0" android:toAlpha="0" android:duration="300"/> </set>
主程序:
package com.example.test01; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MotionEvent; import android.view.View; import android.view.View.OnFocusChangeListener; import android.view.View.OnTouchListener; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; public class MainActivity extends Activity { TextView textView01; Spinner spinner01; private static final String[] countriesStr = {"北京市" , "上海市" , "天津市" , "重庆市"}; private ArrayAdapter<String> adapter; Animation animation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView01 = (TextView)findViewById(R.id.textView01); spinner01 = (Spinner)findViewById(R.id.spinner); adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item , countriesStr); adapter.setDropDownViewResource(R.layout.myspinner_dropdown); //将ArrayAdapter添加进Spinner对象中 spinner01.setAdapter(adapter); //将spinner绑定OnItemSelectedListener spinner01.setOnItemSelectedListener(new SpinnerSelectListener()); animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.spinner_anim); //将spinner绑定OnTouchListener spinner01.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { //将spinner运行Animation v.startAnimation(animation); //将spinner隐藏 v.setVisibility(View.INVISIBLE); return false; } }); spinner01.setOnFocusChangeListener(new OnFocusChangeListener() { public void onFocusChange(View v, boolean hasFocus) { } }); } class SpinnerSelectListener implements OnItemSelectedListener { public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { //将所选spinner的值带入textView中 textView01.setText("您选择的是:" + countriesStr[arg2]); arg0.setVisibility(View.VISIBLE); } public void onNothingSelected(AdapterView<?> arg0) {} } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
扩展:
Animation主要有两种动态方式,一种是tweened animation(渐变动画),另一种是frame by animation(画面转换动画)。tweened animation则有一下4种基本转换方式:
>Alpha-Aniamtion(Transparency changes):透明度转换
>Rotate-Animation(rotations):旋转转换
>Scale-Aniamtion(growing shrinking):缩放转换
>Translate-Animation(position changed):位置转换
定义好你想要的动画XML后,用AnimationUtils.loadAnimation将动画加载,并试图在想要加上动态效果的组件中使用startAnimation方法