Android自訂PopupWindow小案例_Android

來源:互聯網
上載者:User

 PopupWindow是我們開發中的常客之一,使用起來也比較簡單方便。
 寫了個最簡單地自訂PopupWindow,記錄下來,方便以後使用時直接在上面改動就可以。

/** * @param * @author ldm * @description 自訂PopupWindow * @time 2016/9/29 15:26 */public class CustomPopup extends PopupWindow {  //上下文  private Context mContext;  // PopupWindow中控制項點擊事件回調介面  private IPopuWindowListener mOnClickListener;  //PopupWindow布局檔案中的Button  private Button alarm_pop_btn;   /**   * @description 構造方法   * @author ldm   * @time 2016/9/30 9:14   * @param   */  public CustomPopup(Context mContext, int width, int height, IPopuWindowListener listener) {    super(mContext);    this.mContext = mContext;    this.mOnClickListener = listener;    //擷取布局檔案    View mContentView = LayoutInflater.from(mContext).inflate(R.layout.alarm_disopse_pop, null);    //設定布局    setContentView(mContentView);    // 設定彈窗的寬度和高度    setWidth(width);    setHeight(height);    //設定能否擷取到焦點    setFocusable(false);    //設定PopupWindow進入和退出時的動畫效果    setAnimationStyle(R.style.popwindow_exit_anim_style);    setTouchable(true); // 預設是true,設定為false,所有touch事件無響應,而被PopupWindow覆蓋的Activity部分會響應點擊    // 設定彈窗外可點擊,此時點擊PopupWindow外的範圍,Popupwindow不會消失    setOutsideTouchable(false);    //外部是否可以點擊,設定Drawable原因可以參考:http://blog.csdn.net/harvic880925/article/details/49278705    setBackgroundDrawable(new BitmapDrawable());    // 設定彈窗的布局介面    initUI();  }  /**   * 初始化彈窗列表   */  private void initUI() {  //擷取到按鈕    alarm_pop_btn = (Button) getContentView().findViewById(R.id.alarm_pop_btn);    //設定按鈕點擊事件    alarm_pop_btn.setOnClickListener(new View.OnClickListener() {      @Override      public void onClick(View view) {        if (null != mOnClickListener) {          mOnClickListener.dispose();        }      }    });  }  /**   * 顯示彈窗列表介面   */  public void show(View view) {    int[] location = new int[2];    view.getLocationOnScreen(location);    //Gravity.BOTTOM設定在view下方,還可以根據location來設定PopupWindowj顯示的位置    showAtLocation(view, Gravity.BOTTOM, 0, 0);  }  /**   * @param   * @author ldm   * @description 點擊事件回調處理介面   * @time 2016/7/29 15:30   */  public interface IPopuWindowListener {    void dispose();  }}

註:設定PopupWindow的寬高還可以通過LayoutParams來設定,比如:

 //通過LayoutParams來設定PopupWindow的高度和寬度  ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams)mContentView.getLayoutParams();   lp.widht=400;   lp.height = 180;  mContentView.setLayoutParams(lp);  //但是直接這樣寫擷取到的lp可能為空白,我們在擷取一個View的LayoutParams時,通常應該這樣寫:  //在addOnGlobalLayoutListener監聽中來擷取View的LayoutParams  mContentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {      @Override      public void onGlobalLayout() {        ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) mContentView.getLayoutParams();        lp.height = 180;        lp.width = 400;        mContentView.setLayoutParams(lp);      }    });

在Activity中使用:

private CustomPopup alarmPopup;....//初始化PopupWindow這裡通過資料200來設定PopupWindow高度alarmPopup=new CustomPopup(getActivity(), ViewGroup.LayoutParams.MATCH_PARENT, 200, this);//這裡的this是指當前Activity實現了PopupWindow中IPopuWindowListener介面//彈出PopupWindow @Override  protected void widgetClick(View v) {    super.widgetClick(v);    switch (v.getId()) {      case R.id.popup:        alarmPopup.show(v);        break;    }  }  //處理PopupWindow中按鈕點擊回調   @Override  public void dispose() {    //TODO sth     if (alarmPopup.isShowing()) {        alarmPopup.dismiss();//關閉PopupWindow      }  }

PopupWindow對應 的布局介面:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:id="@+id/pop_ll"  android:layout_width="match_parent"  android:layout_height="60dp"  android:background="#404040"  android:orientation="horizontal">  <Button    android:id="@+id/alarm_pop_btn"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@null"    android:gravity="center"    android:text="@string/dispose"    android:textColor="#FFFFFF"    android:textSize="18sp" /></LinearLayout>

動畫style:

  <style name="popwindow_exit_anim_style" parent="android:Animation">    <item name="android:windowEnterAnimation">@anim/popshow_anim</item>    <!-- 指定顯示的動畫xml -->    <item name="android:windowExitAnimation">@anim/pophidden_anim</item>    <!-- 指定消失的動畫xml -->  </style>

popshow_anim.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >  <translate  android:duration="500"  android:fromYDelta="100%p"  android:toYDelta="0" /> <alpha  android:duration="500"  android:fromAlpha="0.0"  android:toAlpha="1.0" /></set>

pophidden_anim.xml:

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >   <translate  android:duration="500"  android:fromYDelta="0"  android:toYDelta="50%p" /> <alpha  android:duration="500"  android:fromAlpha="1.0"  android:toAlpha="0.0" /></set>

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.