2019独角兽企业重金招聘Python工程师标准>>> 

项目做到一期收尾阶段,突然要用到弹出窗口,于是自然而然的就想起了PopupWindow这个组件了,才发现平时用的少,对这个组件几乎是完全无知的状态啊。
于是恶补一番
现在放出学习成果——————请无视展示效果
救我理解来看,PopupWindow 其实关键来说就是个Window容器,既然是容器,那么必然有他自己的宽高,但是内容就由着开发者自己定义了。
所以第一步 先定义两个变量
PopupWindow pop;//弹出窗口的容器View view; //容器内放置的内容
然后我们来看初始化方法:
private void initPopupWindow(String json) {if (json == null || json.equals("")) {ToastUtil.WarnImageToast(Phone_MainActivity.this, "今日油价获取失败","short");return;}String oil_text_90;String oil_text_93;String oil_text_97;String oil_text_0;OilBean ob = gson.fromJson(json, OilBean.class);oil_text_0 = ob.getShowapi_res_body().getList().get(0).getP0();oil_text_90 = ob.getShowapi_res_body().getList().get(0).getP90();oil_text_93 = ob.getShowapi_res_body().getList().get(0).getP93();oil_text_97 = ob.getShowapi_res_body().getList().get(0).getP97();view = this.getLayoutInflater().inflate(R.layout.popup_window, null);CustomRLayout oil90 = (CustomRLayout) view.findViewById(R.id.oil_90_layout);CustomRLayout oil93 = (CustomRLayout) view.findViewById(R.id.oil_93_layout);CustomRLayout oil97 = (CustomRLayout) view.findViewById(R.id.oil_97_layout);CustomRLayout oil0 = (CustomRLayout) view.findViewById(R.id.oil_0_layout);oil90.setRightText("¥" + oil_text_90);oil93.setRightText("¥" + oil_text_93);oil97.setRightText("¥" + oil_text_97);oil0.setRightText("¥" + oil_text_0);pop = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);pop.setAnimationStyle(R.style.popwin_anim_style);pop.setOutsideTouchable(false);view.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubpop.dismiss();}});}
整个过程非常简单:
首先判断传入的网络json是有数据的,然后对Json数据进行解析,将解析后的数据放入到指定的View的组件中去。
注意观察可以发现,在这个方法中用到了一个布局 R.layout.popup_window
我们来看看这个布局吧:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:custom="http://schemas.android.com/apk/res/com.dhcc.gpscarmanager_phone"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@drawable/oil_window_shape"android:gravity="center_horizontal"android:orientation="vertical" ><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:padding="10dip"android:text="今日油价"android:textColor="@color/white"android:textSize="@dimen/text_normal_size" /><Viewandroid:layout_width="match_parent"android:layout_height="1dip"android:layout_margin="2dip"android:background="@color/line_gray" /><com.dhcc.gpscarmanager_phone.CustomView.CustomRLayoutandroid:id="@+id/oil_90_layout"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="2dip"android:layout_marginRight="2dip"android:background="@color/red"custom:leftText="90#汽油"custom:leftTextColor="@color/white"custom:leftTextSize="@dimen/text_small_size"custom:rightText="¥6.98"custom:rightTextColor="@color/white"custom:rightTextSize="@dimen/text_small_size" /><Viewandroid:layout_width="match_parent"android:layout_height="1dip"android:layout_margin="2dip"android:background="@color/line_gray" /><com.dhcc.gpscarmanager_phone.CustomView.CustomRLayoutandroid:id="@+id/oil_93_layout"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="2dip"android:layout_marginRight="2dip"custom:leftText="93#汽油"custom:leftTextColor="@color/white"custom:leftTextSize="@dimen/text_small_size"custom:rightText="¥5.92"custom:rightTextColor="@color/white"custom:rightTextSize="@dimen/text_small_size" /><Viewandroid:layout_width="match_parent"android:layout_height="1dip"android:layout_margin="2dip"android:background="@color/line_gray" /><com.dhcc.gpscarmanager_phone.CustomView.CustomRLayoutandroid:id="@+id/oil_97_layout"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="2dip"android:layout_marginRight="2dip"custom:leftText="97#汽油"custom:leftTextColor="@color/white"custom:leftTextSize="@dimen/text_small_size"custom:rightText="¥7.62"custom:rightTextColor="@color/white"custom:rightTextSize="@dimen/text_small_size" /><Viewandroid:layout_width="match_parent"android:layout_height="1dip"android:layout_margin="2dip"android:background="@color/line_gray" /><com.dhcc.gpscarmanager_phone.CustomView.CustomRLayoutandroid:id="@+id/oil_0_layout"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="2dip"android:layout_marginRight="2dip"custom:leftText="柴油"custom:leftTextColor="@color/white"custom:leftTextSize="@dimen/text_small_size"custom:rightText="¥6.28"custom:rightTextColor="@color/white"custom:rightTextSize="@dimen/text_small_size" /><Viewandroid:layout_width="match_parent"android:layout_height="1dip"android:layout_margin="2dip"android:background="@color/line_gray" /></LinearLayout>
很简单的垂直线性布局,具体效果看那个红色的窗口就知道了。
然后就是控制一下PopupWIndow在界面的什么位置显示了。
这里是写在OnclickListener中的
case R.id.main_tab02:/** getIntentTool().intent_to(Phone_MainActivity.this,* Mix_RankList_Activity.class);*/if (pop != null && pop.isShowing()) {pop.dismiss();} else if (pop != null) {pop.showAtLocation(arg0, Gravity.BOTTOM, 0, 0);// popupWindow居中显示} else if (pop == null) {ToastUtil.WarnImageToast(Phone_MainActivity.this, "油价获取中","short");}break;
判断一下窗口是否初始化成功,并且判断窗口是否在显示中,这里要注意以下,为了让窗口显示在界面底部,代码需要这样写:
pop.showAtLocation(arg0, Gravity.BOTTOM, 0, 0);// popupWindow底部显示pop.showAtLocation(arg0, Gravity.CENTER, 0, 0);// popupWindow底部显示
这样一个基本的PopupWindow基本上就做好了
但是还是没有动画效果
如何加如动画效果呢?
这里要用到Style和属性动画的概念了:
进入屏幕动画:popupwindow_in.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:fromXDelta="0"android:toXDelta="0"android:fromYDelta="3000"android:toYDelta="0"android:duration="500" /></set>
500毫秒内,从组件位于屏幕的Y轴3000的方向向屏幕的0点方向移动
离开屏幕动画就是把进入动画反过来就好了:
popupwindow_out.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:fromXDelta="0"android:toXDelta="0"android:fromYDelta="0"android:toYDelta="3000"android:duration="500" /></set>
然后在Style中定义一下这个动画切换效果:
<style name="popwin_anim_style"><item name="android:windowEnterAnimation">@anim/popupwindow_in</item><item name="android:windowExitAnimation">@anim/popupwindow_out</item></style>
这样数据就可以显示了
最后在PopupWindow中初始化调用时设置一下就好了,具体参见方法: initPopupWindow中的一代码:
pop.setAnimationStyle(R.style.popwin_anim_style);
至此,一个带动画的PopupWindow就写完了,整体的逻辑思路是这样的:
1.先定义布局界面,确定Window中要显示什么。
2.再初始化PopupWindow,确定其显示的相关参数,并将自定义的View与PopupWindow进行绑定。
3.确定Window的触发条件,并确定窗口弹出位置
4.在anim中加入动画效果,并在Style中定义改动画效果。
5.随后在初始化中利用setAnimationStyle方法设置动画。
来自为知笔记(Wiz)