ActionSheetForAndroid安卓版本的ActionSheet

泡在网上的日子 发表于 2014-09-05 15:57 第 3986 次阅读 ActionSheet,开源
0

编辑推荐:稀土掘金,这是一个针对技术开发者的一个应用,你可以在掘金上获取最新最优质的技术干货,不仅仅是Android知识、前端、后端以至于产品和设计都有涉猎,想成为全栈工程师的朋友不要错过!

模仿IOS里面的UIActionSheet控件,有IOS6和IOS7两种风格,可以自定义风格,背景图片、按钮图片、文字颜色、间距等。

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

ActionSheetActionSheet

使用方法

创建一个ActionSheet并显示

1
2
3
4
5
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))

1
2
3
4
5
6
7
8
9
@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

1
2
3
4
5
6
<!-- Application theme. -->
     <style name= "AppTheme" parent= "AppBaseTheme" >
         <item name= "actionSheetStyle" >@style/ActionSheetStyleIOS6</item>
         or
         <item name= "actionSheetStyle" >@style/ActionSheetStyleIOS7</item>
     </style>

还可以自定义样式,自定义一个style即可,可以参考ActionSheetStyleIOS6/ActionSheetStyleIOS7的写法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- 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选项按钮文字颜色


相关文章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值