自訂PopupWindow,popupwindow

來源:互聯網
上載者:User

自訂PopupWindow,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" >    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:clickable="true"        android:gravity="center"        android:textColor="@android:color/holo_orange_dark"        android:text="確定" />    <TextView        android: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">    <!-- 平移    <translate         android:duration="5000"         android:fromXDelta="100%"         android:toXDelta="0"/>         -->    <scale        android:fromXScale="0"        android:fromYScale="0"        android:pivotX="50%"        android:pivotY="50%"        android:toXScale="0.8"        android:toYScale="0.5"        android:duration="200"/>    <!--fromXScalefromYScale起始時X,Y座標,pivotXpivotY動畫起始位置,相對於螢幕的百分比,兩個都為50%表示動畫從螢幕中間開始toXScaletoYScale動畫最終縮放的倍數, 1.0為正常大小,大於1.0放大duration動畫期間 -->    <!--透明度-->    <alpha        android: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">   <!-- <translate        android:duration="5000"        android:fromXDelta="0"        android:toXDelta="100%"/>-->    <scale        android:fromXScale="0.8"        android:fromYScale="0.5"        android:pivotX="50%"        android:pivotY="50%"        android:toXScale="0"        android:toYScale="0"        android:duration="200"/>    <alpha        android: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() {            @Override            public 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() {            @Override            public 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() {            @Override            public 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() {    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.