日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

PopupWindow在项目中的使用 并指定位置及加入动画效果

發(fā)布時(shí)間:2023/12/19 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PopupWindow在项目中的使用 并指定位置及加入动画效果 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

項(xiàng)目做到一期收尾階段,突然要用到彈出窗口,于是自然而然的就想起了PopupWindow這個(gè)組件了,才發(fā)現(xiàn)平時(shí)用的少,對(duì)這個(gè)組件幾乎是完全無(wú)知的狀態(tài)啊。 于是惡補(bǔ)一番 現(xiàn)在放出學(xué)習(xí)成果——————請(qǐng)無(wú)視展示效果
救我理解來(lái)看,PopupWindow 其實(shí)關(guān)鍵來(lái)說(shuō)就是個(gè)Window容器,既然是容器,那么必然有他自己的寬高,但是內(nèi)容就由著開(kāi)發(fā)者自己定義了。
所以第一步 先定義兩個(gè)變量?
  • PopupWindow pop;//彈出窗口的容器
  • View view; //容器內(nèi)放置的內(nèi)容

  • 然后我們來(lái)看初始化方法:
  • private void initPopupWindow(String json) {
  • if (json == null || json.equals("")) {
  • ToastUtil.WarnImageToast(Phone_MainActivity.this, "今日油價(jià)獲取失敗",
  • "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() {
  • @Override
  • public void onClick(View v) {
  • // TODO Auto-generated method stub
  • pop.dismiss();
  • }
  • });
  • }
  • 整個(gè)過(guò)程非常簡(jiǎn)單: 首先判斷傳入的網(wǎng)絡(luò)json是有數(shù)據(jù)的,然后對(duì)Json數(shù)據(jù)進(jìn)行解析,將解析后的數(shù)據(jù)放入到指定的View的組件中去。 注意觀察可以發(fā)現(xiàn),在這個(gè)方法中用到了一個(gè)布局 R.layout.popup_window 我們來(lái)看看這個(gè)布局吧:
  • <?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" >
  • <TextView
  • android:layout_width="match_parent"
  • android:layout_height="wrap_content"
  • android:gravity="center"
  • android:padding="10dip"
  • android:text="今日油價(jià)"
  • android:textColor="@color/white"
  • android:textSize="@dimen/text_normal_size" />
  • <View
  • android:layout_width="match_parent"
  • android:layout_height="1dip"
  • android:layout_margin="2dip"
  • android:background="@color/line_gray" />
  • <com.dhcc.gpscarmanager_phone.CustomView.CustomRLayout
  • android: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" />
  • <View
  • android:layout_width="match_parent"
  • android:layout_height="1dip"
  • android:layout_margin="2dip"
  • android:background="@color/line_gray" />
  • <com.dhcc.gpscarmanager_phone.CustomView.CustomRLayout
  • android: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" />
  • <View
  • android:layout_width="match_parent"
  • android:layout_height="1dip"
  • android:layout_margin="2dip"
  • android:background="@color/line_gray" />
  • <com.dhcc.gpscarmanager_phone.CustomView.CustomRLayout
  • android: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" />
  • <View
  • android:layout_width="match_parent"
  • android:layout_height="1dip"
  • android:layout_margin="2dip"
  • android:background="@color/line_gray" />
  • <com.dhcc.gpscarmanager_phone.CustomView.CustomRLayout
  • android: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" />
  • <View
  • android:layout_width="match_parent"
  • android:layout_height="1dip"
  • android:layout_margin="2dip"
  • android:background="@color/line_gray" />
  • </LinearLayout>
  • 很簡(jiǎn)單的垂直線性布局,具體效果看那個(gè)紅色的窗口就知道了。
    然后就是控制一下PopupWIndow在界面的什么位置顯示了。 這里是寫(xiě)在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, "油價(jià)獲取中",
  • "short");
  • }
  • break;
  • 判斷一下窗口是否初始化成功,并且判斷窗口是否在顯示中,這里要注意以下,為了讓窗口顯示在界面底部,代碼需要這樣寫(xiě):
  • pop.showAtLocation(arg0, Gravity.BOTTOM, 0, 0);// popupWindow底部顯示
  • pop.showAtLocation(arg0, Gravity.CENTER, 0, 0);// popupWindow底部顯示
  • 這樣一個(gè)基本的PopupWindow基本上就做好了 但是還是沒(méi)有動(dòng)畫(huà)效果 如何加如動(dòng)畫(huà)效果呢? 這里要用到Style和屬性動(dòng)畫(huà)的概念了:
    進(jìn)入屏幕動(dòng)畫(huà):popupwindow_in.xml
  • <?xml version="1.0" encoding="utf-8"?>
  • <set xmlns:android="http://schemas.android.com/apk/res/android">
  • <translate
  • android:fromXDelta="0"
  • android:toXDelta="0"
  • android:fromYDelta="3000"
  • android:toYDelta="0"
  • android:duration="500" />
  • </set>
  • 500毫秒內(nèi),從組件位于屏幕的Y軸3000的方向向屏幕的0點(diǎn)方向移動(dòng) 離開(kāi)屏幕動(dòng)畫(huà)就是把進(jìn)入動(dòng)畫(huà)反過(guò)來(lái)就好了: popupwindow_out.xml
  • <?xml version="1.0" encoding="utf-8"?>
  • <set xmlns:android="http://schemas.android.com/apk/res/android">
  • <translate
  • android:fromXDelta="0"
  • android:toXDelta="0"
  • android:fromYDelta="0"
  • android:toYDelta="3000"
  • android:duration="500" />
  • </set>
  • 然后在Style中定義一下這個(gè)動(dòng)畫(huà)切換效果:
  • <style name="popwin_anim_style">
  • <item name="android:windowEnterAnimation">@anim/popupwindow_in</item>
  • <item name="android:windowExitAnimation">@anim/popupwindow_out</item>
  • </style>
  • 這樣數(shù)據(jù)就可以顯示了 最后在PopupWindow中初始化調(diào)用時(shí)設(shè)置一下就好了,具體參見(jiàn)方法: initPopupWindow中的一代碼:
  • pop.setAnimationStyle(R.style.popwin_anim_style);
  • 至此,一個(gè)帶動(dòng)畫(huà)的PopupWindow就寫(xiě)完了,整體的邏輯思路是這樣的:
    1.先定義布局界面,確定Window中要顯示什么。 2.再初始化PopupWindow,確定其顯示的相關(guān)參數(shù),并將自定義的View與PopupWindow進(jìn)行綁定。 3.確定Window的觸發(fā)條件,并確定窗口彈出位置 4.在anim中加入動(dòng)畫(huà)效果,并在Style中定義改動(dòng)畫(huà)效果。 5.隨后在初始化中利用setAnimationStyle方法設(shè)置動(dòng)畫(huà)。



    來(lái)自為知筆記(Wiz)

    轉(zhuǎn)載于:https://my.oschina.net/t5xgkow/blog/510476

    總結(jié)

    以上是生活随笔為你收集整理的PopupWindow在项目中的使用 并指定位置及加入动画效果的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。