转载请注明出处:http://blog.csdn.net/bbld_/article/details/39124097
前言
这几天用到了github上的一个仿IOS的ActionSheet(ActionSheetForAndroid)控件,它是使用Fragment实现的,由于我的主界面也是由几个Fragment来做,所以这样会造成FragmentManager出现一些问题。所以就把它的实现方式改为由自定义Dialog来实现,保留它的主题属性方便修改样式,同时去除Fragmrnt部分,增加可用性。
实现
package com.roc.actionsheet;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
/**
* ActionSheet
*
* @author Mr.Zheng
* @date 2014年9月7日 下午11:15:54
*/
public class ActionSheet extends Dialog implements OnClickListener
{
/* 控件的id */
private static final int CANCEL_BUTTON_ID = 100;
private static final int BG_VIEW_ID = 10;
private static final int TRANSLATE_DURATION = 300;
private static final int ALPHA_DURATION = 300;
private Context mContext;
private Attributes mAttrs;
private MenuItemClickListener mListener;
private View mView;
private LinearLayout mPanel;
private View mBg;
private List<String> items;
private String cancelTitle = "";
private boolean mCancelableOnTouchOutside;
private boolean mDismissed = true;
private boolean isCancel = true;
public ActionSheet(Context context)
{
super(context, android.R.style.Theme_Light_NoTitleBar);// 全屏
this.mContext = context;
initViews();
getWindow().setGravity(Gravity.BOTTOM);
Drawable drawable = new ColorDrawable();
drawable.setAlpha(0);// 设置透明背景
getWindow().setBackgroundDrawable(drawable);
}
public void initViews()
{
/* 隐藏软键盘 */
InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive())
{
View focusView = ((Activity) mContext).getCurrentFocus();
if (focusView != null)
imm.hideSoftInputFromWindow(focusView.getWindowToken(), 0);
}
mAttrs = readAttribute();// 获取主题属性
mView = createView();
mBg.startAnimation(createAlphaInAnimation());
mPanel.startAnimation(createTranslationInAnimation());
}
private Animation createTranslationInAnimation()
{
int type = TranslateAnimation.RELATIVE_TO_SELF;
TranslateAnimation an = new TranslateAnimation(type, 0, type, 0, type, 1, type, 0);
an.setDuration(TRANSLATE_DURATION);
return an;
}
priva