1,定义Dialog
View view = inflater.inflate(R.layout.phone_dialog, null);
final Dialog builder = new Dialog(this, R.style.ActionSheetDialogStyle);
builder.setCanceledOnTouchOutside(true);
builder.setContentView(view);
--- 设置dialog占满屏幕
- Window dialogWindow = dialog.getWindow();
- WindowManager.LayoutParams lp = dialogWindow.getAttributes();
- dialogWindow.setGravity(Gravity.CENTER);
- lp.width = LayoutParams.FILL_PARENT;
- dialogWindow.setAttributes(lp);
builder.show();
R.style.ActionSheetDialogStyle :
<!-- 自定义仿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>
--------------------------
ActionSheetDialogAnimation:
<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>
---------------------------
@anim/actionsheet_dialog_in:
<?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" />
@anim/actionsheet_dialog_out
<?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%" />
-----------------------------
ActionSheetDialog actionSheetDialog = new ActionSheetDialog(CreateActivitiesActivity.this);
actionSheetDialog.builder();
actionSheetDialog.setCancelable(true);
actionSheetDialog.setCanceledOnTouchOutside(true);
actionSheetDialog.addSheetItem("讲座", SheetItemColor.Blue,
new OnSheetItemClickListener() {
@Override
public void onClick(int which) {
tv_activityType.setText("讲座");
}
});
actionSheetDialog.addSheetItem("聚会", SheetItemColor.Blue,
new OnSheetItemClickListener() {
@Override
public void onClick(int which) {
tv_activityType.setText("聚会");
}
});
actionSheetDialog.addSheetItem("亲子游", SheetItemColor.Blue,
new OnSheetItemClickListener() {
@Override
public void onClick(int which) {
tv_activityType.setText("亲子游");
}
});
actionSheetDialog.show();
-----------
ActionSheetDialog :
package org.yuedi.mamafan.widget;
import java.util.ArrayList;
import java.util.List;
import org.yuedi.mamafan.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.toast_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() {
@Override
public 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 = 1; i <= size; i++) {
final int index = i;
SheetItem sheetItem = sheetItemList.get(i - 1);
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 (size == 1) {
if (showTitle) {
textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector);
} else {
textView.setBackgroundResource(R.drawable.actionsheet_single_selector);
}
} else {
if (showTitle) {
if (i >= 1 && i < size) {
textView.setBackgroundResource(R.drawable.actionsheet_middle_selector);
} else {
textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector);
}
} else {
if (i == 1) {
textView.setBackgroundResource(R.drawable.actionsheet_top_selector);
} else if (i < size) {
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() {
@Override
public 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;
}
}
}
--------------------------------------------
2,popupwindow
private void showPopupWindow(View view) {
View popupView = View.inflate(this, R.layout.fetale_ducation_guide, null);
popupWindow = new PopupWindow(this);
WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
int xPos = windowManager.getDefaultDisplay().getWidth() / 2;
popupWindow.setWidth(xPos);
popupWindow.setHeight(200);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
// 设置此参数获得焦点,否则无法点击
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setContentView(popupView);
popupWindow.showAsDropDown(view, xPos, 0);
}
-----------------------------------------------
3,activity设置成dialog类型的,用的就是dialog的style
<style name="MyDialogStyleBottom" parent="android:Theme.Dialog">
<item name="android:windowAnimationStyle">@style/AnimBottom</item>
<item name="android:windowFrame">@null</item>
<!-- 边框 -->
<item name="android:windowIsFloating">true</item>
<!-- 是否浮现在activity之上 -->
<item name="android:windowIsTranslucent">true</item>
<!-- 半透明 -->
<item name="android:windowNoTitle">true</item>
<!-- 无标题 -->
<item name="android:windowBackground">@android:color/transparent</item>
<!-- 背景透明 -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 模糊 -->
</style>