Android 底部彈出選擇選單ActionSheet
阿新 • • 發佈:2018-12-18
對於各式各樣的選擇選單彈框可以使用很多中方式來實現,下面我們來看看ActionSheet是如何實現的:
一.第三方ActionSheet實現方法:
compile 'com.baoyz.actionsheet:library:1.1.4'
1.新增以上依賴,實現下面程式碼,寫在對應的監聽事件操作中即可:
ActionSheet.createBuilder(MainActivity.this, getSupportFragmentManager()) .setCancelButtonTitle("取消(Cancel)") .setOtherButtonTitles("開啟相簿(Open Gallery)", "拍照(Camera)", "裁剪(Crop)", "編輯(Edit)") .setCancelableOnTouchOutside(true) .setListener(new ActionSheet.ActionSheetListener() { @Override public void onDismiss(ActionSheet actionSheet, boolean isCancel) { } @Override public void onOtherButtonClick(ActionSheet actionSheet, int index) { String path = "/sdcard/pk1-2.jpg"; switch (index) { case 0: if (mutiSelect) { GalleryFinal.openGalleryMuti(REQUEST_CODE_GALLERY, functionConfig, mOnHanlderResultCallback); } else { GalleryFinal.openGallerySingle(REQUEST_CODE_GALLERY, functionConfig, mOnHanlderResultCallback); } break; case 1: GalleryFinal.openCamera(REQUEST_CODE_CAMERA, functionConfig, mOnHanlderResultCallback); break; case 2: if (new File(path).exists()) { GalleryFinal.openCrop(REQUEST_CODE_CROP, functionConfig, path, mOnHanlderResultCallback); } else { Toast.makeText(MainActivity.this, "圖片不存在", Toast.LENGTH_SHORT).show(); } break; case 3: if (new File(path).exists()) { GalleryFinal.openEdit(REQUEST_CODE_EDIT, functionConfig, path, mOnHanlderResultCallback); } else { Toast.makeText(MainActivity.this, "圖片不存在", Toast.LENGTH_SHORT).show(); } break; default: break; } } }) .show();
判斷中的邏輯可以忽略,由於是專案中的,所有不方便貼出來,主要是實現圖中的效果
2.在style.xml中加入配置:
<!-- iOS7 Style --> <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>
3.在主題AppTheme中新增關聯屬性:
<item name="actionSheetStyle">@style/ActionSheetStyleiOS7</item>
新增位置如下:
<!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="actionSheetStyle">@style/ActionSheetStyleiOS7</item> </style>
二.使用Dialog方法實現:
1.使用Dialog的實現方式,解決原ActionSheet使用Fragment實現而出現的部分手機取消按鈕被遮蓋的問題:
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:
//設定對應的邏輯操作即可
break;
case 1:
break;
case 2:
break;
default:
break;
}
}
}).show();
部分資原始檔:
a.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>
b.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>
c.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>
d.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>