Android 天气APP(八)城市切换 之 自定义弹窗与使用,灵魂拷问

mPopupWindow.setContentView(mView);

mPopupWindow.setOutsideTouchable(true);//点击空白处不关闭弹窗 true为关闭

mPopupWindow.setFocusable(true);

mPopupWindow.setAnimationStyle(R.style.AnimationRightFade); //设置动画

mPopupWindow.showAtLocation(mView, Gravity.RIGHT,0 ,0);

setBackgroundAlpha(0.5f,mContext);

WindowManager.LayoutParams nomal = ((Activity) mContext).getWindow().getAttributes();

nomal.alpha = 0.5f;

((Activity) mContext).getWindow().setAttributes(nomal);

mPopupWindow.setOnDismissListener(closeDismiss);

}

/**

  • 底部显示

  • @param mView

*/

public void showBottomPopupWindow(View mView) {

mPopupWindow = new PopupWindow(mView,

ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

mPopupWindow.setContentView(mView);

mPopupWindow.setOutsideTouchable(true);//点击空白处不关闭弹窗 true为关闭

mPopupWindow.setFocusable(true);

mPopupWindow.setAnimationStyle(R.style.AnimationBottomFade); //设置动画

mPopupWindow.showAtLocation(mView, Gravity.BOTTOM, 0, 0);

setBackgroundAlpha(0.5f,mContext);

WindowManager.LayoutParams nomal = ((Activity) mContext).getWindow().getAttributes();

nomal.alpha = 0.5f;

((Activity) mContext).getWindow().setAttributes(nomal);

mPopupWindow.setOnDismissListener(closeDismiss);

}

public static void setBackgroundAlpha(float bgAlpha,Context mContext){

WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();

lp.alpha = bgAlpha;

((Activity) mContext).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

((Activity) mContext).getWindow().setAttributes(lp);

}

/**

  • 设置弹窗动画

  • @param animId

  • @return showPopu

*/

public LiWindow setAnim(int animId) {

if (mPopupWindow != null) {

mPopupWindow.setAnimationStyle(animId);

}

return mLiWindow;

}

//弹窗消失时关闭阴影

public PopupWindow.OnDismissListener closeDismiss = new PopupWindow.OnDismissListener() {

@Override

public void onDismiss() {

WindowManager.LayoutParams nomal = ((Activity)mContext).getWindow().getAttributes();

nomal.alpha = 1f;

((Activity)mContext).getWindow().setAttributes(nomal);

}

};

public void closePopupWindow() {

if (mPopupWindow != null) {

mPopupWindow.dismiss();

}

}

/*

使用方法

  • LiWindow liWindow = new LiWindow(MainActivity.this);

View mView = LayoutInflater.from(MainActivity.this).inflate(R.layout.center_layout,null);

liWindow.showCenterPopupWindow(mView);

  • */

}

弹窗也是需要布局文件的,现在创建一个新的布局文件,用于显示城市列表。

返回图标:

在这里插入图片描述

在项目的layout下创建一个名为window_city_list.xml的布局文件

代码如下:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:app=“http://schemas.android.com/apk/res-auto”

android:orientation=“vertical”

android:fitsSystemWindows=“true”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”>

<LinearLayout

android:orientation=“vertical”

android:background=“#FFF”

android:layout_width=“240dp”

android:layout_height=“match_parent”>

<RelativeLayout

android:layout_width=“match_parent”

android:layout_height=“wrap_content”>

<androidx.appcompat.widget.Toolbar

android:layout_width=“match_parent”

android:layout_height=“?attr/actionBarSize”

app:contentInsetLeft=“16dp”

app:popupTheme=“@style/AppTheme.PopupOverlay”>

<TextView

android:id=“@+id/tv_title”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_gravity=“center”

android:textSize=“16sp”

android:textColor=“#000”

android:text=“中国” />

</androidx.appcompat.widget.Toolbar>

<

  • 16
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值