---
title: "ActionSheetForAndroid"
author: "xiaobojava"
date: "2015-05-06T17:50:46Z"
tags: []
categories: ["移动开发"]
slug: "actionsheetforandroid"
draft: false
lastmod: "2015-05-09T17:11:36Z"
id: 2208935
url: "https://zmx.iteye.com/blog/2208935"
---

本文引自：https://github.com/baoyongzhang/ActionSheetForAndroid

 

# ActionSheetForAndroid

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

[![Screenshot](https://raw.githubusercontent.com/baoyongzhang/ActionSheetForAndroid/master/screenshot-1.png)](https://raw.githubusercontent.com/baoyongzhang/ActionSheetForAndroid/master/screenshot-1.png)    [![Screenshot](https://raw.githubusercontent.com/baoyongzhang/ActionSheetForAndroid/master/screenshot-2.png)](https://raw.githubusercontent.com/baoyongzhang/ActionSheetForAndroid/master/screenshot-2.png)

 

# 使用方法

### [](https://github.com/baoyongzhang/ActionSheetForAndroid#%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AAactionsheet%E5%B9%B6%E6%98%BE%E7%A4%BA)创建一个ActionSheet并显示

```
ActionSheet.createBuilder(this, getSupportFragmentManager())
                .setCancelButtonTitle("Cancel")
                .setOtherButtonTitles("Item1", "Item2", "Item3", "Item4")
                .setCancelableOnTouchOutside(true)
                .setListener(this).show();
```

### [](https://github.com/baoyongzhang/ActionSheetForAndroid#%E6%96%B9%E6%B3%95%E8%AF%B4%E6%98%8E)方法说明

- `setCancelButtonTitle()` 设置取消按钮的标题
- `setOtherButtonTitles()` 设置条目，String\[]
- `setCancelableOnTouchOutside()` 设置点击空白处关闭
- `setListener()` 设置事件监听器
- `show()` 返回`ActionSheet`对象，可以调用`ActionSheet`对象的`dismiss()`方法手动关闭

### [](https://github.com/baoyongzhang/ActionSheetForAndroid#%E4%BA%8B%E4%BB%B6%E7%9B%91%E5%90%AC)事件监听

实现`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();
    }
```

### [](https://github.com/baoyongzhang/ActionSheetForAndroid#%E6%A0%B7%E5%BC%8F)样式

默认的样式非常丑陋，项目中提供了两种Style，可以配置Theme

```
<!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="actionSheetStyle">@style/ActionSheetStyleIOS6</item>
        or
        <item name="actionSheetStyle">@style/ActionSheetStyleIOS7</item>
    </style>
```

另一个方式就是配置使用这个组件的Activity中设置Theme

```
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
		
	setTheme(R.style.ActionSheetStyleIOS7);
	setContentView(R.layout.ac_upload_id_photo);
}
```

 

还可以自定义样式，自定义一个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>
```

### [](https://github.com/baoyongzhang/ActionSheetForAndroid#style%E5%B1%9E%E6%80%A7%E4%BB%8B%E7%BB%8D)Style属性介绍

- `actionSheetBackground` 背景
- `cancelButtonBackground` 取消按钮背景
- `otherButtonTopBackground` 选项顶部按钮背景
- `otherButtonMiddleBackground` 选项中部按钮背景
- `otherButtonBottomBackground` 选项底部按钮背景
- `otherButtonSingleBackground` 选项只有一个的按钮背景
- `cancelButtonTextColor` 取消按钮的文字颜色
- `otherButtonTextColor` 选项按钮的文字颜色
- `actionSheetPadding` 内边距
- `otherButtonSpacing` 选项按钮的间距
- `cancelButtonMarginTop` 取消按钮顶部间距
- `actionSheetTextSize` 选项按钮文字颜色