ActionSheetForAndroid安卓版本的ActionSheet
來自: http://www.jcodecraeer.com//a/anzhuokaifa/androidkaifa/2014/0905/1677.html
模仿IOS里面的UIActionSheet控件,有IOS6和IOS7兩種風格,可以自定義風格,背景圖片、按鈕圖片、文字顏色、間距等。
項目地址:https://github.com/baoyongzhang/ActionSheetForAndroid
使用方法
創建一個ActionSheet并顯示
ActionSheet.createBuilder(this, getSupportFragmentManager()) .setCancelButtonTitle("Cancel") .setOtherButtonTitles("Item1", "Item2", "Item3", "Item4") .setCancelableOnTouchOutside(true) .setListener(this).show();
方法說明
-
setCancelButtonTitle()
設置取消按鈕的標題 -
setOtherButtonTitles()
設置條目,String[] -
setCancelableOnTouchOutside()
設置點擊空白處關閉 -
setListener()
設置事件監聽器 -
show()
返回ActionSheet
對象,可以調用ActionSheet
對象的dismiss()
方法手動關閉
事件監聽
實現ActionSheetListener
接口
-
onOtherButtonClick()
點擊某個條目,index
是條目的下標 -
onDismiss()
關閉事件,isCancel
參數表示是否是點擊取消按鈕、返回鍵、或者點擊空白處(setCancelableOnTouchOutside(true)
)
@Override public void onOtherButtonClick(ActionSheet actionSheet, int index) { Toast.makeText(getApplicationContext(), "click item index = " + index, 0).show(); } @Override public void onDismiss(ActionSheet actionSheet, boolean isCancle) { Toast.makeText(getApplicationContext(), "dismissed isCancle = " + isCancle, 0).show(); }
樣式
默認的樣式非常丑陋,項目中提供了兩種Style,可以配置Theme
<!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <item name="actionSheetStyle">@style/ActionSheetStyleIOS6</item> or <item name="actionSheetStyle">@style/ActionSheetStyleIOS7</item> </style>
還可以自定義樣式,自定義一個style即可,可以參考ActionSheetStyleIOS6/ActionSheetStyleIOS7的寫法
<!-- IOS7樣式 --> <style name="ActionSheetStyleIOS7"> <item name="actionSheetBackground">@android:color/transparent</item> <item name="cancelButtonBackground">@drawable/slt_as_ios7_cancel_bt</item> <item name="otherButtonTopBackground">@drawable/slt_as_ios7_other_bt_top</item> <item name="otherButtonMiddleBackground">@drawable/slt_as_ios7_other_bt_middle</item> <item name="otherButtonBottomBackground">@drawable/slt_as_ios7_other_bt_bottom</item> <item name="otherButtonSingleBackground">@drawable/slt_as_ios7_other_bt_single</item> <item name="cancelButtonTextColor">#1E82FF</item> <item name="otherButtonTextColor">#1E82FF</item> <item name="actionSheetPadding">10dp</item> <item name="otherButtonSpacing">0dp</item> <item name="cancelButtonMarginTop">10dp</item> <item name="actionSheetTextSize">12sp</item> </style>
Style屬性介紹
-
actionSheetBackground
背景 -
cancelButtonBackground
取消按鈕背景 -
otherButtonTopBackground
選項頂部按鈕背景 -
otherButtonMiddleBackground
選項中部按鈕背景 -
otherButtonBottomBackground
選項底部按鈕背景 -
otherButtonSingleBackground
選項只有一個的按鈕背景 -
cancelButtonTextColor
取消按鈕的文字顏色 -
otherButtonTextColor
選項按鈕的文字顏色 -
actionSheetPadding
內邊距 -
otherButtonSpacing
選項按鈕的間距 -
cancelButtonMarginTop
取消按鈕頂部間距 -
actionSheetTextSize
選項按鈕文字顏色