ActionSheetForAndroid安卓版本的ActionSheet

Z1wvHr8BYfW 8年前發布 | 10K 次閱讀 Android開發 移動開發

來自: http://www.jcodecraeer.com//a/anzhuokaifa/androidkaifa/2014/0905/1677.html


模仿IOS里面的UIActionSheet控件,有IOS6和IOS7兩種風格,可以自定義風格,背景圖片、按鈕圖片、文字顏色、間距等。

項目地址:https://github.com/baoyongzhang/ActionSheetForAndroid

ActionSheetActionSheet

使用方法

創建一個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選項按鈕文字顏色


 本文由用戶 Z1wvHr8BYfW 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!