直接效果图:

实现步骤:

1.主界面activity_main.xml非常easy,一个button

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/skin_background" >    <Button        android:id="@+id/show"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:gravity="center"        android:text="退出系统"        android:textSize="20sp" /></RelativeLayout>
2.弹出层样式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="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/content" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/actionsheet_bottom_selector" android:gravity="center" android:text="@string/content" android:textColor="#FD4A2E" 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>

3.ActionSheet类

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 mContent = (TextView) layout.findViewById(R.id.content);TextView mCancel = (TextView) layout.findViewById(R.id.cancel);mContent.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubactionSheetSelected.onClick(0);dlg.dismiss();}});mCancel.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubactionSheetSelected.onClick(1);dlg.dismiss();}});Window w = dlg.getWindow();WindowManager.LayoutParams lp = w.getAttributes();lp.x = 0;final int cMakeBottom = 0;lp.y = cMakeBottom;lp.gravity = Gravity.CENTER;dlg.onWindowAttributesChanged(lp);dlg.setCanceledOnTouchOutside(false);if (cancelListener != null)dlg.setOnCancelListener(cancelListener);dlg.setContentView(layout);dlg.show();return dlg;}}
4.MainActivity

public class MainActivity extends Activity implements OnActionSheetSelected, OnCancelListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViewById(R.id.show).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubActionSheet.showSheet(MainActivity.this, MainActivity.this, MainActivity.this);}});}@Overridepublic void onClick(int whichButton) {// TODO Auto-generated method stubswitch (whichButton) {case 0:showToast("确定");break;case 1:showToast("取消");break;default:break;}}@Overridepublic void onCancel(DialogInterface dialog) {// TODO Auto-generated method stubshowToast("取消");}private void showToast(CharSequence charSequence) {Toast.makeText(this, charSequence, Toast.LENGTH_SHORT).show();}}

源代码下载地址



版权声明:本文博客原创文章,博客,未经同意,不得转载。

更多相关文章

  1. Android札记【2】——系统的认识心中的Android[首届 Google 暑期
  2. 【新版】Android技术博客精华汇总
  3. GitHub 优秀的 Android 开源项目 淘宝技术牛p博客整理开发中最常
  4. 收藏一个不错的Android原创博客
  5. android技术牛人的博客
  6. 利用OpenCV在Android上DIY相机——第二届 Google 暑期大学生博客
  7. Android新浪微博客户端—开篇
  8. Android经典的大牛博客推荐
  9. Android Gradle Plugin指南(一)——简介 - 琴弦第七的专栏 - 博客

随机推荐

  1. Android之系统给我们提供的常见的Activit
  2. Android里的媒体库
  3. 演化理解 Android 异步加载图片
  4. Android系统调试(02)ANR问题总结
  5. TextView 文字描边
  6. Google Developing for Android 二 - Memo
  7. 谷歌Android为何关闭源代码?
  8. Android——EditText【输入框】的所有属
  9. 在Android中使用Lua脚本 —— Programing
  10. android中的weight