仿android4.0 Spinner下拉效果
1.自定義控件需求
自從android4.0發布後,android的桌麵效果進一步得到增強以及美化,增加了動畫特效,可是這僅僅用於android4.0以上的版本,
對於很多停留在android2.3或者更低的版本時,很多隻有感歎。
為了獲得更好的用戶體驗,很多軟件產品在設計時,已經考慮到在android4.0以下版本加入android4.0以上版本的特效,那麼
自定義控件來達到效果。
2.效果展示
3.技術點
1.自定義控件spinner包含Button和PopupWindow控件;
2.當點擊Button時,展示PopupWindow控件;
3.點擊PopupWindow的布局控件時,隱藏,並設置Button;
4.代碼展示
1.主界麵
1 public class MainActivity extends Activity { 2 3 private SpinnerButton mSpinnerBtn; 4 5 @Override 6 public void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.main); 9 10 this.mSpinnerBtn = (SpinnerButton) this 11 .findViewById(R.id.spinner_btn); 12 13 // 設置下拉布局資源文件,布局創建監聽器,以便實例化控件對象 14 mSpinnerBtn.setResIdAndViewCreatedListener(R.layout.spinner_dropdown_items, 15 new SpinnerButton.ViewCreatedListener(){ 16 @Override 17 public void onViewCreated(View v) { 18 19 // TODO Auto-generated method stub 20 v.findViewById(R.id.textView1).setOnClickListener(new View.OnClickListener() { 21 @Override 22 public void onClick(View v) { 23 // TODO Auto-generated method stub 24 handleClick(((TextView)v).getText().toString()); 25 } 26 }); 27 v.findViewById(R.id.textView2).setOnClickListener(new View.OnClickListener() { 28 @Override 29 public void onClick(View v) { 30 // TODO Auto-generated method stub 31 handleClick(((TextView)v).getText().toString()); 32 } 33 }); 34 v.findViewById(R.id.textView3).setOnClickListener(new View.OnClickListener() { 35 @Override 36 public void onClick(View v) { 37 // TODO Auto-generated method stub 38 handleClick(((TextView)v).getText().toString()); 39 } 40 }); 41 v.findViewById(R.id.textView4).setOnClickListener(new View.OnClickListener() { 42 @Override 43 public void onClick(View v) { 44 // TODO Auto-generated method stub 45 handleClick(((TextView)v).getText().toString()); 46 } 47 }); 48 } 49 50 }); 51 } 52 53 private void handleClick(String text){ 54 mSpinnerBtn.dismiss(); 55 mSpinnerBtn.setText(text); 56 } 57 }
2.自定義控件Spinner
1 /** 2 * @ClassName SpinnerButton 3 * @Description TODO 防android4.0 Spinner下拉效果 4 * @author kenny 5 * @date 2012-8-14 6 */ 7 public class SpinnerButton extends Button { 8 9 private Context mContext; 10 /** 下拉PopupWindow */ 11 private UMSpinnerDropDownItems mPopupWindow; 12 /** 下拉布局文件ResourceId */ 13 private int mResId; 14 /** 下拉布局文件創建監聽器 */ 15 private ViewCreatedListener mViewCreatedListener; 16 17 public SpinnerButton(Context context, AttributeSet attrs, int defStyle) { 18 super(context, attrs, defStyle); 19 initButton(context); 20 } 21 22 public SpinnerButton(Context context, AttributeSet attrs) { 23 super(context, attrs); 24 initButton(context); 25 } 26 public SpinnerButton(Context context, final int resourceId, 27 ViewCreatedListener mViewCreatedListener) { 28 super(context); 29 setResIdAndViewCreatedListener(resourceId, mViewCreatedListener); 30 initButton(context); 31 } 32 33 private void initButton(Context context) { 34 this.mContext = context; 35 // UMSpinnerButton監聽事件 36 setOnClickListener(new UMSpinnerButtonOnClickListener()); 37 } 38 39 public PopupWindow getPopupWindow() { 40 return mPopupWindow; 41 } 42 43 public void setPopupWindow(UMSpinnerDropDownItems mPopupWindow) { 44 this.mPopupWindow = mPopupWindow; 45 } 46 47 public int getResId() { 48 return mResId; 49 } 50 /** 51 * @Description: TODO 隱藏下拉布局 52 */ 53 public void dismiss(){ 54 mPopupWindow.dismiss(); 55 } 56 /** 57 * @Description: TODO 設置下拉布局文件,及布局文件創建監聽器 58 * @param @param mResId 下拉布局文件ID 59 * @param @param mViewCreatedListener 布局文件創建監聽器 60 */ 61 public void setResIdAndViewCreatedListener(int mResId, ViewCreatedListener mViewCreatedListener) { 62 this.mViewCreatedListener = mViewCreatedListener; 63 // 下拉布局文件id 64 this.mResId = mResId; 65 // 初始化PopupWindow 66 mPopupWindow = new UMSpinnerDropDownItems(mContext); 67 } 68 69 /** 70 * UMSpinnerButton的點擊事件 71 */ 72 class UMSpinnerButtonOnClickListener implements View.OnClickListener { 73 74 @Override 75 public void onClick(View v) { 76 if (mPopupWindow != null) { 77 if (!mPopupWindow.isShowing()) { 78 // 設置PopupWindow彈出,退出樣式 79 mPopupWindow.setAnimationStyle(R.style.Animation_dropdown); 80 // 計算popupWindow下拉x軸的位置 81 int lx = (SpinnerButton.this.getWidth() 82 - mPopupWindow.getmViewWidth() - 7) / 2; 83 // showPopupWindow 84 mPopupWindow.showAsDropDown(SpinnerButton.this, lx, -5); 85 } 86 } 87 } 88 } 89 90 /** 91 * @ClassName UMSpinnerDropDownItems 92 * @Description TODO 下拉界麵 93 * @author kenny 94 * @date 2012-8-14 95 */ 96 public class UMSpinnerDropDownItems extends PopupWindow { 97 98 private Context mContext; 99 /** 下拉視圖的寬度 */ 100 private int mViewWidth; 101 /** 下拉視圖的高度 */ 102 private int mViewHeight; 103 104 public UMSpinnerDropDownItems(Context context) { 105 super(context); 106 this.mContext = context; 107 loadViews(); 108 } 109 110 /** 111 * @Description: TODO 加載布局文件 112 * @param 113 * @return void 114 * @throws 115 */ 116 private void loadViews() { 117 // 布局加載器加載布局文件 118 LayoutInflater inflater = LayoutInflater.from(mContext); 119 final View v = inflater.inflate(mResId, null); 120 // 計算view寬高 121 onMeasured(v); 122 123 // 必須設置 124 setWidth(LayoutParams.WRAP_CONTENT); 125 setHeight(LayoutParams.WRAP_CONTENT); 126 setContentView(v); 127 setFocusable(true); 128 129 // 設置布局創建監聽器,以便在實例化布局控件對象 130 if (mViewCreatedListener != null) { 131 mViewCreatedListener.onViewCreated(v); 132 } 133 } 134 135 /** 136 * @Description: TODO 計算View長寬 137 * @param @param v 138 */ 139 private void onMeasured(View v) { 140 int w = View.MeasureSpec.makeMeasureSpec(0, 141 View.MeasureSpec.UNSPECIFIED); 142 int h = View.MeasureSpec.makeMeasureSpec(0, 143 View.MeasureSpec.UNSPECIFIED); 144 v.measure(w, h); 145 mViewWidth = v.getMeasuredWidth(); 146 mViewHeight = v.getMeasuredHeight(); 147 } 148 149 public int getmViewWidth() { 150 return mViewWidth; 151 } 152 153 public void setmViewWidth(int mViewWidth) { 154 this.mViewWidth = mViewWidth; 155 } 156 157 public int getmViewHeight() { 158 return mViewHeight; 159 } 160 161 public void setmViewHeight(int mViewHeight) { 162 this.mViewHeight = mViewHeight; 163 } 164 165 } 166 /** 167 * @ClassName ViewCreatedListener 168 * @Description TODO 布局創建監聽器,實例化布局控件對象 169 * @author kenny 170 * @date 2012-8-15 171 */ 172 public interface ViewCreatedListener { 173 void onViewCreated(View v); 174 } 175 }
注意:在public UMSpinnerDropDownItems(Context context)構造函數裏,不能繼承super(context);否則會出現下拉框黑邊的情況;
由於時間關係,其他代碼就不一一貼出來了,要的話直接去下載工程 源代碼下載
最後更新:2017-04-04 07:03:48