实现类似iOS中的UIActionSheet效果

本文只列出核心实现代码,具体实现请参考附件。

 

布局如下:

<?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="wrap_content"
    android:background="@color/transparent"
    android:orientation="vertical"
    android:padding="5dp" >

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/actionsheet_top_normal"
        android:gravity="center"
        android:text="@string/title"
        android:textColor="#8F8F8F"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/smallTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/actionsheet_middle_selector"
        android:gravity="center"
        android:text="@string/small"
        android:textColor="#000000"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/middleTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/actionsheet_middle_selector"
        android:gravity="center"
        android:text="@string/middle"
        android:textColor="#000000"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/bigTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/actionsheet_bottom_selector"
        android:gravity="center"
        android:text="@string/big"
        android:textColor="#000000"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/cancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@drawable/actionsheet_single_selector"
        android:gravity="center"
        android:text="@string/cancel"
        android:textColor="#037BFF"
        android:textSize="16sp" />

</LinearLayout>

 

具体实现如下:

package com.eric.actionsheet;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface.OnCancelListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ActionSheet {

	public interface OnActionSheetSelected {
		void onClick(int whichButton);
	}

	private ActionSheet() {
	}

	public static Dialog showSheet(Context context,final OnActionSheetSelected actionSheetSelected,OnCancelListener cancelListener) {
		final Dialog dlg = new Dialog(context, R.style.ActionSheet);
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.actionsheet, null);
		final int cFullFillWidth = 10000;
		layout.setMinimumWidth(cFullFillWidth);

		TextView smallContent = (TextView) layout.findViewById(R.id.smallTextView);
		TextView middleContent = (TextView) layout.findViewById(R.id.middleTextView);
		TextView bigContent = (TextView) layout.findViewById(R.id.bigTextView);
		TextView mCancel = (TextView) layout.findViewById(R.id.cancel);

		smallContent.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				actionSheetSelected.onClick(0);
				dlg.dismiss();
			}
		});
		middleContent.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				actionSheetSelected.onClick(1);
				dlg.dismiss();
			}
		});
		bigContent.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				actionSheetSelected.onClick(2);
				dlg.dismiss();
			}
		});

		mCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				actionSheetSelected.onClick(3);
				dlg.dismiss();
			}
		});

		Window w = dlg.getWindow();
		WindowManager.LayoutParams lp = w.getAttributes();
		lp.x = 0;
		final int cMakeBottom = -1000;
		lp.y = cMakeBottom;
		lp.gravity = Gravity.BOTTOM;
		dlg.onWindowAttributesChanged(lp);
		dlg.setCanceledOnTouchOutside(false);
		if (cancelListener != null)
			dlg.setOnCancelListener(cancelListener);

		dlg.setContentView(layout);
		dlg.show();

		return dlg;
	}

}

 

调用示例:

ActionSheet.showSheet(MainActivity.this, MainActivity.this, MainActivity.this);

 实现类似iOS中的UIActionSheet效果

你可能感兴趣的:(android,ActionSheet)