欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 中Popwindow彈出菜單的兩種方法實例

 更新時間:2017年03月20日 15:13:38   作者:ztp800201  
這篇文章主要介紹了Android 中Popwindow彈出菜單的兩種方法實例的相關資料,這里提供了兩種實現(xiàn)的方法,并附有實例代碼,需要的朋友可以參考下

Android 中Popwindow彈出菜單的兩種方法實例

1.popWindow就是對話框的一種方式!

此文講解的android中對話框的一種使用方式,它叫popWindow。

2、popWindow的特性

Android的對話框有兩種:PopupWindow和AlertDialog。它們的不同點在于:

AlertDialog的位置固定,而PopupWindow的位置可以隨意。

AlertDialog是非阻塞線程的,而PopupWindow是阻塞線程的。

PopupWindow的位置按照有無偏移分,可以分為偏移和無偏移兩種;按照參照物的不同,可以分為相對于某個控件(Anchor錨)和相對于父控件。

實例代碼:

方法一的Activity

package com.app.test02; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.View.OnTouchListener; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.Button; 
import android.widget.PopupWindow; 
import android.widget.Toast; 
 
public class PopwindowLeft extends Activity { 
  // 聲明PopupWindow對象的引用 
  private PopupWindow popupWindow; 
 
  /** Called when the activity is first created. */ 
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_popupwindow_main); 
    // 點擊按鈕彈出菜單 
    Button pop = (Button) findViewById(R.id.popBtn); 
    pop.setOnClickListener(popClick); 
  } 
 
  // 點擊彈出左側菜單的顯示方式 
  OnClickListener popClick = new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
      // TODO Auto-generated method stub 
      getPopupWindow(); 
      // 這里是位置顯示方式,在屏幕的左側 
      popupWindow.showAtLocation(v, Gravity.LEFT, 0, 0); 
    } 
  }; 
 
  /** 
   * 創(chuàng)建PopupWindow 
   */ 
  protected void initPopuptWindow() { 
    // TODO Auto-generated method stub 
    // 獲取自定義布局文件activity_popupwindow_left.xml的視圖 
    View popupWindow_view = getLayoutInflater().inflate(R.layout.activity_popupwindow_left, null, 
        false); 
    // 創(chuàng)建PopupWindow實例,200,LayoutParams.MATCH_PARENT分別是寬度和高度 
    popupWindow = new PopupWindow(popupWindow_view, 200, LayoutParams.MATCH_PARENT, true); 
    // 設置動畫效果 
    popupWindow.setAnimationStyle(R.style.AnimationFade); 
    // 點擊其他地方消失 
    popupWindow_view.setOnTouchListener(new OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
        // TODO Auto-generated method stub 
        if (popupWindow != null && popupWindow.isShowing()) { 
          popupWindow.dismiss(); 
          popupWindow = null; 
        } 
        return false; 
      } 
    }); 
  } 
  /*** 
   * 獲取PopupWindow實例 
   */ 
  private void getPopupWindow() { 
    if (null != popupWindow) { 
      popupWindow.dismiss(); 
      return; 
    } else { 
      initPopuptWindow(); 
    } 
  } 
} 

方法二的Activity

package com.app.test02; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.View.OnTouchListener; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.PopupWindow; 
 
public class PopwindowLeftNew extends Activity{ 
  private PopupWindow popupWindow; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_popupwindow_main); 
     
    findViewById(R.id.popBtn).setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        // TODO Auto-generated method stub 
        // 獲取自定義布局文件activity_popupwindow_left.xml的視圖 
        View popupWindow_view = getLayoutInflater().inflate(R.layout.activity_popupwindow_left, null,false); 
        // 創(chuàng)建PopupWindow實例,200,LayoutParams.MATCH_PARENT分別是寬度和高度 
        popupWindow = new PopupWindow(popupWindow_view, 200, LayoutParams.MATCH_PARENT, true); 
        // 設置動畫效果 
        popupWindow.setAnimationStyle(R.style.AnimationFade); 
        // 這里是位置顯示方式,在屏幕的左側 
        popupWindow.showAtLocation(v, Gravity.LEFT, 0, 0); 
        // 點擊其他地方消失 
        popupWindow_view.setOnTouchListener(new OnTouchListener() { 
          @Override 
          public boolean onTouch(View v, MotionEvent event) { 
            // TODO Auto-generated method stub 
            if (popupWindow != null && popupWindow.isShowing()) { 
              popupWindow.dismiss(); 
              popupWindow = null; 
            } 
            return false; 
          } 
        }); 
      } 
    }); 
     
  } 
} 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關文章

最新評論