高仿IOS---Dialog(底部式)

来源:互联网 发布:nginx配置静态页面 编辑:程序博客网 时间:2024/05/17 01:09

代码地址 点击打开链接



activity_main.xml

<LinearLayout 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:orientation="vertical" >    <Button        android:id="@+id/btn1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="消息" />    <Button        android:id="@+id/btn2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="图片" />    <Button        android:id="@+id/btn3"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="列表" /></LinearLayout>

view_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:orientation="vertical"    android:padding="8dp" >    <TextView        android:id="@+id/txt_title"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@drawable/actionsheet_top_normal"        android:gravity="center"        android:minHeight="45dp"        android:paddingBottom="10dp"        android:paddingLeft="15dp"        android:paddingRight="15dp"        android:paddingTop="10dp"        android:textColor="#8F8F8F"        android:textSize="13sp"        android:visibility="gone" />    <ScrollView        android:id="@+id/sLayout_content"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:fadingEdge="none" >        <LinearLayout            android:id="@+id/lLayout_content"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="vertical" >        </LinearLayout>    </ScrollView>    <TextView        android:id="@+id/txt_cancel"        android:layout_width="match_parent"        android:layout_height="45dp"        android:layout_marginTop="8dp"        android:background="@drawable/actionsheet_single_selector"        android:gravity="center"        android:text="取消"        android:textColor="#037BFF"        android:textSize="18sp"        android:textStyle="bold" /></LinearLayout>

    <!-- 自定义仿IOS的ActionSheet底部Dialog的样式 ,有模糊效果 -->    <style name="ActionSheetDialogStyle" parent="@android:style/Theme.Dialog">        <!-- 背景透明 -->        <item name="android:windowBackground">@android:color/transparent</item>        <item name="android:windowContentOverlay">@null</item>        <!-- 浮于Activity之上 -->        <item name="android:windowIsFloating">true</item>        <!-- 边框 -->        <item name="android:windowFrame">@null</item>        <!-- Dialog以外的区域模糊效果 -->        <item name="android:backgroundDimEnabled">true</item>        <!-- 无标题 -->        <item name="android:windowNoTitle">true</item>        <!-- 半透明 -->        <item name="android:windowIsTranslucent">true</item>        <!-- Dialog进入及退出动画 -->        <item name="android:windowAnimationStyle">@style/ActionSheetDialogAnimation</item>    </style>    <!-- ActionSheet进出动画 -->    <style name="ActionSheetDialogAnimation" parent="@android:style/Animation.Dialog">        <item name="android:windowEnterAnimation">@anim/actionsheet_dialog_in</item>        <item name="android:windowExitAnimation">@anim/actionsheet_dialog_out</item>    </style>

MainActivity

package com.zf.iosdialog;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;import com.zf.iosdialog.widget.ActionSheetDialog;import com.zf.iosdialog.widget.ActionSheetDialog.OnSheetItemClickListener;import com.zf.iosdialog.widget.ActionSheetDialog.SheetItemColor;public class MainActivity extends Activity implements OnClickListener {private Button btn1;private Button btn2;private Button btn3;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();}private void initView() {btn1 = (Button) findViewById(R.id.btn1);btn1.setOnClickListener(this);btn2 = (Button) findViewById(R.id.btn2);btn2.setOnClickListener(this);btn3 = (Button) findViewById(R.id.btn3);btn3.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn1:new ActionSheetDialog(MainActivity.this).builder().setTitle("清空消息列表后,聊天记录依然保留,确定要清空消息列表?")//.setCancelable(false)//可以点击其他地方消失窗口.setCanceledOnTouchOutside(true).addSheetItem("清空消息列表", SheetItemColor.Red,new OnSheetItemClickListener() {@Overridepublic void onClick(int which) {}}).show();break;case R.id.btn2:new ActionSheetDialog(MainActivity.this).builder()//这样也可以点击其他地方消失窗口.setCancelable(true)//.setCanceledOnTouchOutside(false).addSheetItem("发送给好友", SheetItemColor.Blue,new OnSheetItemClickListener() {@Overridepublic void onClick(int which) {}}).addSheetItem("转载到空间相册", SheetItemColor.Blue,new OnSheetItemClickListener() {@Overridepublic void onClick(int which) {}}).addSheetItem("上传到群相册", SheetItemColor.Blue,new OnSheetItemClickListener() {@Overridepublic void onClick(int which) {}}).addSheetItem("保存到手机", SheetItemColor.Blue,new OnSheetItemClickListener() {@Overridepublic void onClick(int which) {}}).show();break;case R.id.btn3:new ActionSheetDialog(MainActivity.this).builder().setTitle("请选择操作").setCancelable(false).setCanceledOnTouchOutside(false).addSheetItem("条目一", SheetItemColor.Blue,new OnSheetItemClickListener() {@Overridepublic void onClick(int which) {Toast.makeText(MainActivity.this,"item" + which, Toast.LENGTH_SHORT).show();}}).addSheetItem("条目二", SheetItemColor.Blue,new OnSheetItemClickListener() {@Overridepublic void onClick(int which) {Toast.makeText(MainActivity.this,"item" + which, Toast.LENGTH_SHORT).show();}}).addSheetItem("条目三", SheetItemColor.Blue,new OnSheetItemClickListener() {@Overridepublic void onClick(int which) {Toast.makeText(MainActivity.this,"item" + which, Toast.LENGTH_SHORT).show();}}).addSheetItem("条目四", SheetItemColor.Blue,new OnSheetItemClickListener() {@Overridepublic void onClick(int which) {Toast.makeText(MainActivity.this,"item" + which, Toast.LENGTH_SHORT).show();}}).addSheetItem("条目五", SheetItemColor.Blue,new OnSheetItemClickListener() {@Overridepublic void onClick(int which) {Toast.makeText(MainActivity.this,"item" + which, Toast.LENGTH_SHORT).show();}}).addSheetItem("条目六", SheetItemColor.Blue,new OnSheetItemClickListener() {@Overridepublic void onClick(int which) {Toast.makeText(MainActivity.this,"item" + which, Toast.LENGTH_SHORT).show();}}).addSheetItem("条目七", SheetItemColor.Blue,new OnSheetItemClickListener() {@Overridepublic void onClick(int which) {Toast.makeText(MainActivity.this,"item" + which, Toast.LENGTH_SHORT).show();}}).addSheetItem("条目八", SheetItemColor.Blue,new OnSheetItemClickListener() {@Overridepublic void onClick(int which) {Toast.makeText(MainActivity.this,"item" + which, Toast.LENGTH_SHORT).show();}}).addSheetItem("条目九", SheetItemColor.Blue,new OnSheetItemClickListener() {@Overridepublic void onClick(int which) {Toast.makeText(MainActivity.this,"item" + which, Toast.LENGTH_SHORT).show();}}).addSheetItem("条目十", SheetItemColor.Blue,new OnSheetItemClickListener() {@Overridepublic void onClick(int which) {Toast.makeText(MainActivity.this,"item" + which, Toast.LENGTH_SHORT).show();}}).show();break;default:break;}}}

ActionSheetDialog

package com.zf.iosdialog.widget;import java.util.ArrayList;import java.util.List;import com.zf.iosdialog.R;import android.app.Dialog;import android.content.Context;import android.graphics.Color;import android.view.Display;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.LinearLayout.LayoutParams;import android.widget.ScrollView;import android.widget.TextView;public class ActionSheetDialog {private Context context;private Dialog dialog;private TextView txt_title;private TextView txt_cancel;private LinearLayout lLayout_content;private ScrollView sLayout_content;private boolean showTitle = false;private List<SheetItem> sheetItemList;private Display display;public ActionSheetDialog(Context context) {this.context = context;WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);display = windowManager.getDefaultDisplay();}public ActionSheetDialog builder() {// 获取Dialog布局View view = LayoutInflater.from(context).inflate(R.layout.view_actionsheet, null);// 设置Dialog最小宽度为屏幕宽度view.setMinimumWidth(display.getWidth());// 获取自定义Dialog布局中的控件sLayout_content = (ScrollView) view.findViewById(R.id.sLayout_content);lLayout_content = (LinearLayout) view.findViewById(R.id.lLayout_content);txt_title = (TextView) view.findViewById(R.id.txt_title);txt_cancel = (TextView) view.findViewById(R.id.txt_cancel);txt_cancel.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {dialog.dismiss();}});// 定义Dialog布局和参数dialog = new Dialog(context, R.style.ActionSheetDialogStyle);dialog.setContentView(view);Window dialogWindow = dialog.getWindow();dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);WindowManager.LayoutParams lp = dialogWindow.getAttributes();lp.x = 0;lp.y = 0;dialogWindow.setAttributes(lp);return this;}public ActionSheetDialog setTitle(String title) {showTitle = true;txt_title.setVisibility(View.VISIBLE);txt_title.setText(title);return this;}public ActionSheetDialog setCancelable(boolean cancel) {dialog.setCancelable(cancel);return this;}public ActionSheetDialog setCanceledOnTouchOutside(boolean cancel) {dialog.setCanceledOnTouchOutside(cancel);return this;}/** *  * @param strItem *            条目名称 * @param color *            条目字体颜色,设置null则默认蓝色 * @param listener * @return */public ActionSheetDialog addSheetItem(String strItem, SheetItemColor color,OnSheetItemClickListener listener) {if (sheetItemList == null) {sheetItemList = new ArrayList<SheetItem>();}sheetItemList.add(new SheetItem(strItem, color, listener));return this;}/** 设置条目布局 */private void setSheetItems() {if (sheetItemList == null || sheetItemList.size() <= 0) {return;}int size = sheetItemList.size();// TODO 高度控制,非最佳解决办法// 添加条目过多的时候控制高度if (size >= 7) {LinearLayout.LayoutParams params = (LayoutParams) sLayout_content.getLayoutParams();params.height = display.getHeight() / 2;sLayout_content.setLayoutParams(params);}// 循环添加条目for (int i = 0; i < size; i++) {final int index = i;SheetItem sheetItem = sheetItemList.get(i);String strItem = sheetItem.name;SheetItemColor color = sheetItem.color;final OnSheetItemClickListener listener = (OnSheetItemClickListener) sheetItem.itemClickListener;TextView textView = new TextView(context);textView.setText(strItem);textView.setTextSize(18);textView.setGravity(Gravity.CENTER);/** * 有标题的 */if (showTitle) {//如果sheetItem不是最后一个if (i >= 0 && i < size-1) {textView.setBackgroundResource(R.drawable.actionsheet_middle_selector);} else {//如果sheetItem是最后一个textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector);}} else {/** * 没有标题的 */if (i == 0) {//如果sheetItem是第一个textView.setBackgroundResource(R.drawable.actionsheet_top_selector);} else if (i < size-1) {textView.setBackgroundResource(R.drawable.actionsheet_middle_selector);} else {textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector);}}// 字体颜色if (color == null) {textView.setTextColor(Color.parseColor(SheetItemColor.Blue.getName()));} else {textView.setTextColor(Color.parseColor(color.getName()));}// 高度float scale = context.getResources().getDisplayMetrics().density;int height = (int) (45 * scale + 0.5f);textView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, height));// 点击事件textView.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {listener.onClick(index);dialog.dismiss();}});lLayout_content.addView(textView);}}public void show() {setSheetItems();dialog.show();}public interface OnSheetItemClickListener {void onClick(int which);}public class SheetItem {String name;OnSheetItemClickListener itemClickListener;SheetItemColor color;public SheetItem(String name, SheetItemColor color,OnSheetItemClickListener itemClickListener) {this.name = name;this.color = color;this.itemClickListener = itemClickListener;}}public enum SheetItemColor {Blue("#037BFF"), Red("#FD4A2E");private String name;private SheetItemColor(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}}}

actionsheet_dialog_in.xml

<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="200"    android:fromYDelta="100%"    android:toYDelta="0" />

actionsheet_dialog_out.xml

<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="200"    android:fromYDelta="0"    android:toYDelta="100%" />

<?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"/>    <item android:drawable="@drawable/actionsheet_bottom_normal"/></selector>

<?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>

<?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>

<?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>


0 0
原创粉丝点击