使用Dialog的实现方式,解决原ActionSheet使用Fragment实现而出现的部分手机取消按钮被遮盖的问题
java部分代码:

import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView; public class ActionSheet {
private Dialog mDialog; public ActionSheet(Context context, View contentView, boolean cancelable, boolean otoCancelable) {
if (context == null)
return;
mDialog = new Dialog(context, R.style.custom_dialog_type);
mDialog.setContentView(contentView);
mDialog.setCancelable(cancelable);
mDialog.setCanceledOnTouchOutside(otoCancelable);
Window window = mDialog.getWindow();
WindowManager m = window.getWindowManager();
Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
WindowManager.LayoutParams p = window.getAttributes(); // 获取对话框当前的参数值
p.width = d.getWidth();
window.setAttributes(p);
window.setGravity(Gravity.BOTTOM); //此处可以设置dialog显示的位置
window.setWindowAnimations(R.style.comment_popwindow_anim_style); //添加动画
} public void dismiss() {
if (mDialog != null && mDialog.isShowing()) {
mDialog.dismiss();
}
} public void show() {
if (mDialog != null && !mDialog.isShowing()) {
mDialog.show();
}
} public static Builder createBuilder(Context context) {
return new Builder(context);
} public static class Builder { private Context mContext;
private String mCancelButtonTitle;
private String[] mOtherButtonTitles;
private boolean mCancelableOnTouchOutside = true;
private ActionSheetListener mListener;
ActionSheet ActionSheet = null; public Builder(Context context) {
mContext = context;
} public Builder setCancelButtonTitle(String title) {
mCancelButtonTitle = title;
return this;
} public Builder setCancelButtonTitle(int strId) {
return setCancelButtonTitle(mContext.getString(strId));
} public Builder setOtherButtonTitles(String... titles) {
mOtherButtonTitles = titles;
return this;
} public Builder setListener(ActionSheetListener listener) {
this.mListener = listener;
return this;
} public Builder setCancelableOnTouchOutside(boolean cancelable) {
mCancelableOnTouchOutside = cancelable;
return this;
} public ActionSheet show() { //创建View,设置监听器等
View view = View.inflate(mContext, R.layout.dialog_actionsheet, null);
final ScrollView scrollLayout = (ScrollView) view.findViewById(R.id.scroll_layout);
final LinearLayout llayOther = (LinearLayout) view.findViewById(R.id.llay_other);
//取消按钮
TextView txtCancel = (TextView) view.findViewById(R.id.txt_cancel);
txtCancel.setText(mCancelButtonTitle);
txtCancel.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
if (ActionSheet != null)
ActionSheet.dismiss();
}
});
//其他按钮
if (mOtherButtonTitles != null && mOtherButtonTitles.length != 0) {
for (int i = 0; i < mOtherButtonTitles.length; i++) {
TextView textView = new TextView(mContext);
textView.setText(mOtherButtonTitles[i]); textView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
textView.setTextColor(0xff1E82FF);
textView.setTextSize(16);//16sp的字体大小转化成px
int padding = (int) (10 * mContext.getResources().getDisplayMetrics().density + 0.5f);//10dp的padding转换成px
textView.setPadding(0, padding, 0, padding);
textView.setGravity(Gravity.CENTER);
textView.setBackgroundResource(getOtherButtonBg(mOtherButtonTitles, i)); final int pos = i;
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null)
mListener.onOtherButtonClick(pos);
if (ActionSheet != null)
ActionSheet.dismiss();
}
});
llayOther.addView(textView);
} /**
* 设置一定条数,不能再撑开,而是变成滑动
*/
scrollLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int minNumWhenScroll = 10;//最小可滑动条数
int childViewCount = llayOther.getChildCount();
int scrollLayoutHeight = 0;
int childHeight = 0;
if (childViewCount == 0) {
scrollLayoutHeight = 0;
} else {
childHeight = llayOther.getChildAt(0).getHeight();
if (childViewCount <= minNumWhenScroll) {
scrollLayoutHeight = childHeight * childViewCount;
} else {
scrollLayoutHeight = childHeight * minNumWhenScroll;
}
}
scrollLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, scrollLayoutHeight));
scrollLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}); }
ActionSheet = new ActionSheet(mContext, view, mCancelableOnTouchOutside, mCancelableOnTouchOutside);
ActionSheet.show(); return ActionSheet;
} public int getOtherButtonBg(String[] titles, int i) {
if (titles.length == 1) {
return R.drawable.slt_as_ios7_other_bt_single;
}
if (titles.length == 2) {
switch (i) {
case 0:
return R.drawable.slt_as_ios7_other_bt_top;
case 1:
return R.drawable.slt_as_ios7_other_bt_bottom;
}
}
if (titles.length > 2) {
if (i == 0) {
return R.drawable.slt_as_ios7_other_bt_top;
}
if (i == (titles.length - 1)) {
return R.drawable.slt_as_ios7_other_bt_bottom;
}
return R.drawable.slt_as_ios7_other_bt_middle;
}
return 0;
}
} public interface ActionSheetListener {
void onOtherButtonClick(int index);
}
}

所用到的布局文件:dialog_actionsheet.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"> <ScrollView
android:id="@+id/scroll_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:scrollbars="none"> <LinearLayout
android:id="@+id/llay_other"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:orientation="vertical"> </LinearLayout>
</ScrollView> <TextView
android:id="@+id/txt_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/slt_as_ios7_cancel_bt"
android:gravity="center"
android:padding="10dp"
android:text="取消"
android:textColor="#1E82FF"
android:textSize="16sp" /> </LinearLayout>

代码中使用:

ActionSheet.createBuilder(mActivity)
.setCancelButtonTitle(
"取消")
.setOtherButtonTitles(
"保密",
"男",
"女")
.setCancelableOnTouchOutside(true)
.setListener(new ActionSheet.ActionSheetListener() { @Override
public void onOtherButtonClick(int index) { switch (index) {
case 0:
setSex("保密");
sexCode = "0";
break;
case 1:
setSex("男");
sexCode = "1";
break;
case 2:
setSex("女");
sexCode = "2";
break;
default:
break;
}
}
}).show();

存在的部分资源文件:
1)slt_as_ios7_other_bt_single.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/actionsheet_single_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/actionsheet_single_normal"/> </selector>

2)slt_as_ios7_other_bt_top.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/actionsheet_top_pressed"android:state_pressed="true"/>
<item android:drawable="@drawable/actionsheet_top_normal"/>
</selector>

3)slt_as_ios7_other_bt_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/actionsheet_bottom_pressed" android:state_pressed="true"/>
</selector>

4)slt_as_ios7_other_bt_middle.xml

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/actionsheet_middle_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/actionsheet_middle_normal"/>
</selector>

为什么使用Builder的形式:由于在项目开发中大量使用了旧版本的ActionSheet,导致更改实现时必须暴露相同的接口及实现才能减少不必要的代码修改导致的工作量,本文只给个思路

相关资源下载
http://download.csdn.net/detail/hai1059876295/9484915

Android仿IOS底部弹出选择菜单ActionSheet的更多相关文章

  1. Android仿ios底部弹出框效果

    准备: public class ActionSheet { public interface OnActionSheetSelected { void onClick(int whichButton ...

  2. 仿iOS底部弹出popUpWindow

    上面为弹出来的效果 popUpWindow布局: <?xml version="1.0" encoding="utf-8"?> <Linear ...

  3. xamarin.android 实现 Activity 底部弹出对话框菜单

    Resources/drawable 下新增如下文件: push_bottom_in.xml <?xml version="1.0" encoding="utf-8 ...

  4. [deviceone开发]-底部弹出选择

    一.简介 个人上传的第一个示例源码,两天空闲时间写的,一点简单组件,写的挺乱还没啥注释,仅供新手学习. 底部弹出选择,可滑动选择选项,如果停留在选项中间,可自动校正位置,加了一点简单的动画效果,需要的 ...

  5. EditorGUILayout.EnumPopup 枚举弹出选择菜单

    http://www.unity蛮牛.com/thread-25490-1-1.html http://www.unity蛮牛.com/m/Script/EditorGUILayout.EnumPop ...

  6. Android BottomSheet:底部弹出Fragment面板(4)

     Android BottomSheet:底部弹出Fragment面板(4) BottomSheet不仅可以弹出轻量级的定制好的面板(见附录文章5,6,7),还可以弹出"重"的 ...

  7. JQuery实现复制数据到剪贴板之各种麻花与右键点击弹出选择菜单

    1.如果小伙伴们只是想实现点击某个按钮(通过click事件)实现复制功能. 那小哥哥我在这里推荐大家使用2个非常好用的插件 (1)clipboard.js:纯js插件,无需flash,相对来说更轻量级 ...

  8. [RN] React Native 自定义 底部 弹出 选择框 实现

    React Native 自定义 底部选择框 实现 效果如图所示: 实现方法: 一.组件封装 CustomAlertDialog.js import React, {Component} from ' ...

  9. Android仿微信进度弹出框的实现方法

    MainActivity: package com.ruru.dialogproject; import android.app.Activity; import android.os.Bundle; ...

随机推荐

  1. 10.php引用(&)详解及注意事项

    <?php function &test() { static $b=0;//申明一个静态变量 $b=$b+1; echo $b; return $b; } $a=test();//这条 ...

  2. Casperjs中fill提交表单遇到的问题

    1.if you access internet with proxy please add             --ignore-ssl-errors=true --ssl-protocol=a ...

  3. [荐][转]为何应该使用 MacOS X(论GUI环境下开发人员对软件的配置与重用)

    一周前我和 Tinyfool 闲聊苹果操作系统,都认为对于开发人员来说,苹果操作系统(MacOS)是上佳的选择.Tinyfool 笔头很快,当即就写了一篇长文章,我则笔头很慢,今天才全部码好.他的文章 ...

  4. 接口测试工具 — jmeter(header与cookie的添加)

    1.header的添加 添加HTTP信息头管理器 填写header 2.添加cookie 添加HTTP Cookie管理器 添加cookie值

  5. 报错:Cannot remove entries from nonexistent file c:\program files\anaconda3\lib\site-packages\easy-install.pth

    Outline 这两天通过“掘金量化终端”跑模型策略,之前装好环境一直ok,可以顺畅的Running~ 下午重装了下 Anaconda,刚才跑的时候提示 缺少“gm”模块 (掘金量化必须包): 就按照 ...

  6. 用python合并N个不同字符集编码的sql文件的实践

    背景:我有一项工作任务是将svn某文件夹日常更新的sql文件(归类到日期命名的文件夹中)拿到数据库中运行. 一開始,我是先把sql文件update下来,用notepad++打开,拷贝每个文本的sql语 ...

  7. android自定义控件(一)MeasureSpec 与 ListView.onMeasure

    A MeasureSpec encapsulates the layout requirements passed from parent to child. Each MeasureSpec rep ...

  8. HDU1506: Largest Rectangle in a Histogram(最大子矩阵,好题动态优化左右边界)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1506 刚开始没考虑时间复杂度,直接敲了,直接tle了,之后没有思路,然后看题解,看见大神写的优化非常棒. ...

  9. ES6简单入门

    let let命令用来声明块级作用域. 以前的JavaScript只有全局作用域和函数作用域, 没有块级作用域. // 示例1: if (1) { var a = "hello"; ...

  10. [ Error 分析] Comparison method violates its general contract!

    public static void main(String[] args) { List<Long> ret = new ArrayList<>(); int n = 103 ...