一、个人觉得还是习惯使用Dialog底部弹窗,而非使用PopWindow底部弹出样式。网上有各种PopWindow的弹窗,在此做出Dialog的弹窗
1、JavaCode
/**
* @since 2015/11/17.
* @author wangly
*/
public class BottomMenuDialog extends Dialog implements android.view.View.OnClickListener {
private View view;
private TextView tv_photolib;
private TextView tv_camera;
private TextView tv_cancel;
private TextView tv_logoff;
private TextView tv_logoff_cancel;
private TextView tv_coverPhoto;
private TextView tv_coverCamera;
private TextView tv_coverCancel;
public CoverListener listener;
public quitListener mListener;
public void setListener(CoverListener listener) {
this.listener = listener;
}
public void setListener(quitListener listener){
this.mListener = listener;
}
/**
*
* @param context
* @param theme 主题样式
* @param item 显示的布局样式
*/
public BottomMenuDialog(Context context, int theme,int item) {
super(context, theme);
Window window = this.getWindow();
if (item==0) {
view = LayoutInflater.from(context).inflate(R.layout.bottom_popup_menu, null);
tv_photolib = (TextView) view.findViewById(R.id.tv_photolib);
tv_camera = (TextView) view.findViewById(R.id.tv_camera);
tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
tv_photolib.setOnClickListener(this);
tv_camera.setOnClickListener(this);
tv_cancel.setOnClickListener(this);
}else if (item == 1) {
view = LayoutInflater.from(context).inflate(R.layout.bottom_popup_logoff_menu, null);
tv_logoff = (TextView) view.findViewById(R.id.tv_logoff);
tv_logoff_cancel = (TextView) view.findViewById(R.id.tv_logoff_cancel);
tv_logoff.setOnClickListener(this);
tv_logoff_cancel.setOnClickListener(this);
}else if (item ==2) {
view = LayoutInflater.from(context).inflate(R.layout.layout_cover_popwindow, null);
tv_coverPhoto = (TextView) view.findViewById(R.id.tv_coverPhoto);
tv_coverCamera = (TextView) view.findViewById(R.id.tv_coverCamera);
tv_coverCancel = (TextView) view.findViewById(R.id.tv_coverCancel);
tv_coverPhoto.setOnClickListener(this);
tv_coverCamera.setOnClickListener(this);
tv_coverCancel.setOnClickListener(this);
}
setContentView(view);
window.getDecorView().setPadding(0, 0, 0, 0); <span style="background-color: rgb(255, 0, 0);">//设置dialog弹出布局全屏MATCH_PARENT</span>
// 这句话起全屏的作用
window.setLayout(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
//Dialog width, width of display - 50
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
// layoutParams.width = context.getResources().getDisplayMetrics().widthPixels - 50;
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(layoutParams);
window.setGravity(Gravity.BOTTOM); //底部弹出
window.setWindowAnimations(R.style.BottomMenuAnim); //设置弹窗动画
}
public interface CoverListener{
/**
* 相册
*/
void photoCallBack();
/**
* 拍照
*/
void cameraCallBack();
/**
* 取消
*/
void cancle();
/**
* 返回键监听
*/
void keyBack();
}
public interface quitListener{
/**
* 退出
*/
void quit();
/**
* 取消
*/
void cancle();
/**
* 返回键监听
*/
void keyBack();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
//头像
case R.id.tv_photolib:
if (listener!=null) {
listener.photoCallBack();
}
break;
case R.id.tv_camera:
if (listener!=null) {
listener.cameraCallBack();
}
break;
case R.id.tv_cancel:
if (listener!=null) {
this.dismiss();
listener.cancle();
}
break;
//封面
case R.id.tv_coverCamera:
if (listener!=null) {
listener.cameraCallBack();
}
break;
case R.id.tv_coverPhoto:
if (listener!=null) {
listener.photoCallBack();
}
break;
case R.id.tv_coverCancel:
if (listener!=null) {
this.dismiss();
listener.cancle();
}
break;
//退出登录
case R.id.tv_logoff:
if (mListener!=null) {
mListener.quit();
}
break;
case R.id.tv_logoff_cancel:
if (mListener!=null) {
mListener.cancle();
}
break;
default:
break;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount() == 0) {
if (listener!=null||mListener!=null) {
listener.keyBack();
mListener.keyBack();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
}
2、anim文件夹下的动画
(1)<pre name="code" class="html">dialog_in_from_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromYDelta="100%p"
android:toYDelta="0" />
<alpha
android:duration="@android:integer/config_mediumAnimTime"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
(2)dialog_out_from_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromYDelta="0"
android:toYDelta="100%p" />
<alpha
android:duration="@android:integer/config_mediumAnimTime"
android:fromAlpha="1.0"
android:toAlpha="0.8" />
</set>
3、Style样式
<style name="BottomMenu" parent="android:Theme.Holo.Light.Dialog"> <!-sdk版本11以上->
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowFrame">@null</item>
</style>
<style name="BottomMenuAnim" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/dialog_in_from_bottom</item>
<item name="android:windowExitAnimation">@anim/dialog_out_from_bottom</item>
</style>
4、调用
if(bottomMenuDialog == null) {
bottomMenuDialog = new BottomMenuDialog(this,R.style.BottomMenu,0);
bottomMenuDialog.show();
}
还有待优化,将其封装为单例模式,方面操作调用,创建实例类
Android Dialog底部弹窗
最新推荐文章于 2021-05-26 11:00:39 发布