創(chuàng)新互聯(lián)公司是專業(yè)的紅山網(wǎng)站建設公司,紅山接單;提供成都網(wǎng)站設計、成都做網(wǎng)站,網(wǎng)頁設計,網(wǎng)站設計,建網(wǎng)站,PHP網(wǎng)站建設等專業(yè)做網(wǎng)站服務;采用PHP框架,可快速的進行紅山網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
要彈出PopupWindow窗口的布局
//當前activity
package com.example.testpopwindow; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; 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 MainActivity extends Activity { private Context mContext = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = this; Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showPopupWindow(view); } }); } private void showPopupWindow(View view) { // 一個自定義的布局,作為顯示的內容 View contentView = LayoutInflater.from(mContext).inflate( R.layout.pop_window, null); // 設置按鈕的點擊事件 Button button = (Button) contentView.findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(mContext, "button is pressed", Toast.LENGTH_SHORT).show(); } }); final PopupWindow popupWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true); popupWindow.setTouchable(true); popupWindow.setTouchInterceptor(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { Log.i("mengdd", "onTouch : "); return false; // 這里如果返回true的話,touch事件將被攔截 // 攔截后 PopupWindow的onTouchEvent不被調用,這樣點擊外部區(qū)域無法dismiss } }); // 如果不設置PopupWindow的背景,無論是點擊外部區(qū)域還是Back鍵都無法dismiss彈框 // 我覺得這里是API的一個bug popupWindow.setBackgroundDrawable(getResources().getDrawable( R.drawable.ic_launcher)); // 設置好參數(shù)之后再show // popupWindow.showAsDropDown(view); popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0); } }