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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

自定义PopupWindow

發布時間:2025/3/21 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义PopupWindow 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

?一、布局

?

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:background="#ffffff"android:padding="20dp" ><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:clickable="true"android:gravity="center"android:textColor="@android:color/holo_orange_dark"android:text="確定" /><TextViewandroid:layout_marginTop="20dp"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="10dp"android:clickable="true"android:gravity="center"android:text="取消" /></LinearLayout>

二、自定義MypopupWindow繼承PopupWindow

public class MyPopupWindow extends PopupWindow {

?

?

三、重寫構造方法與動畫樣式

在styles.xml自定義樣式,動畫

<style name="MyPopupWindow"><item name="android:windowEnterAnimation">@anim/pop_in</item><item name="android:windowExitAnimation">@anim/pop_out</item></style>

?

pop_in <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"><!-- 平移<translateandroid:duration="5000"android:fromXDelta="100%"android:toXDelta="0"/>--><scaleandroid:fromXScale="0"android:fromYScale="0"android:pivotX="50%"android:pivotY="50%"android:toXScale="0.8"android:toYScale="0.5"android:duration="200"/><!-- fromXScale fromYScale 起始時X,Y座標,pivotX pivotY動畫起始位置,相對于屏幕的百分比,兩個都為50%表示動畫從屏幕中間開始toXScale toYScale 動畫最終縮放的倍數, 1.0為正常大小,大于1.0放大duration 動畫持續時間--><!--透明度--><alphaandroid:duration="200"android:fromAlpha="0.0"android:toAlpha="1.0"/></set> pop_out
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"><!-- <translateandroid:duration="5000"android:fromXDelta="0"android:toXDelta="100%"/>--><scaleandroid:fromXScale="0.8"android:fromYScale="0.5"android:pivotX="50%"android:pivotY="50%"android:toXScale="0"android:toYScale="0"android:duration="200"/><alphaandroid:duration="200"android:fromAlpha="1.0"android:toAlpha="0.0"/></set> ?

四、重寫構造方法并設置點擊外部可以消失監聽

super(context);this.mContext=context;//打氣筒mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);//打氣 mContentView = mInflater.inflate(R.layout.layout_dialog,null);//設置View setContentView(mContentView);//設置寬與高 setWidth(WindowManager.LayoutParams.MATCH_PARENT);setHeight(WindowManager.LayoutParams.WRAP_CONTENT);/*** 設置進出動畫*/setAnimationStyle(R.style.MyPopupWindow);/*** 設置背景只有設置了這個才可以點擊外邊和BACK消失*/setBackgroundDrawable(new ColorDrawable());/*** 設置可以獲取集點*/setFocusable(true);/*** 設置點擊外邊可以消失*/setOutsideTouchable(true);/***設置可以觸摸*/setTouchable(true);/*** 設置點擊外部可以消失*/setTouchInterceptor(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {/*** 判斷是不是點擊了外部*/if(event.getAction()==MotionEvent.ACTION_OUTSIDE){return true;}//不是點擊外部return false;}});

?

五、顯示及設置窗口變暗與變亮

public void displayDialog(View view){MyPopupWindow myPopupWindow = new MyPopupWindow(this);myPopupWindow.showAsDropDown(mBtnDispaly,0,0);lightOff();/*** 消失時屏幕變亮*/myPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {@Overridepublic void onDismiss() {WindowManager.LayoutParams layoutParams = getWindow().getAttributes();layoutParams.alpha=1.0f;getWindow().setAttributes(layoutParams);}});}/*** 顯示時屏幕變暗*/private void lightOff() {WindowManager.LayoutParams layoutParams = getWindow().getAttributes();layoutParams.alpha=0.3f;getWindow().setAttributes(layoutParams);}

六、完整

package liu.basedemo.view;import android.content.Context; import android.graphics.drawable.ColorDrawable; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import android.widget.PopupWindow;import liu.basedemo.R;/*** 學習PopupWindow* Created by 劉楠 on 2016/8/1 0001.17:42*/ public class MyPopupWindow extends PopupWindow {Context mContext;private LayoutInflater mInflater;private View mContentView;public MyPopupWindow(Context context) {super(context);this.mContext=context;//打氣筒mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);//打氣 mContentView = mInflater.inflate(R.layout.layout_dialog,null);//設置View setContentView(mContentView);//設置寬與高 setWidth(WindowManager.LayoutParams.MATCH_PARENT);setHeight(WindowManager.LayoutParams.WRAP_CONTENT);/*** 設置進出動畫*/setAnimationStyle(R.style.MyPopupWindow);/*** 設置背景只有設置了這個才可以點擊外邊和BACK消失*/setBackgroundDrawable(new ColorDrawable());/*** 設置可以獲取集點*/setFocusable(true);/*** 設置點擊外邊可以消失*/setOutsideTouchable(true);/***設置可以觸摸*/setTouchable(true);/*** 設置點擊外部可以消失*/setTouchInterceptor(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {/*** 判斷是不是點擊了外部*/if(event.getAction()==MotionEvent.ACTION_OUTSIDE){return true;}//不是點擊外部return false;}});/*** 初始化View與監聽器*/initView();initListener();}private void initView() {}private void initListener() {}}

?

轉載于:https://www.cnblogs.com/liunanjava/p/5726977.html

總結

以上是生活随笔為你收集整理的自定义PopupWindow的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。