2013-08-12 127 views
0

我正在使用PopupWindow。當屏幕方向改變時,我面臨着一個問題PopupWindow破壞。彈出窗口破壞屏幕方向

我需要它不應該破壞屏幕方向的變化。

我爲此活動設置了此項。

<activity 
     android:name=".MainPageActivity" 
     android:configChanges="orientation|keyboardHidden" 
     android:label="@string/app_name" 
     android:windowSoftInputMode="adjustPan" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
</activity> 

代碼

/** 
    * Pop up Window 
    */ 
    private void initatePopUpWindow() { 
     try { 
      LayoutInflater layoutInflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View layout = layoutInflator.inflate(R.layout.popup_new_quote, (ViewGroup) findViewById(R.id.popupNewQuote)); 

      displayMetrics = context.getResources().getDisplayMetrics(); 

      display = ((WindowManager) this.getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); 

      if(!(display.getOrientation() == Configuration.ORIENTATION_LANDSCAPE || display.getOrientation() == Configuration.ORIENTATION_UNDEFINED)) { 
       popupWindow = new PopupWindow(layout, displayMetrics.widthPixels, (int)(displayMetrics.heightPixels * .40f), true); 
      } else 
       popupWindow = new PopupWindow(layout, displayMetrics.widthPixels/2, displayMetrics.heightPixels/2, true); 

      popupWindow.setAnimationStyle(R.style.AnimationPopup); 
      popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0); 

      //New Quote and Quote Name TextView 
      ((TextView)layout.findViewById(R.id.newQuoteTextView)).setTypeface(typeFace); 
      ((TextView)layout.findViewById(R.id.quoteNameTextView)).setTypeface(typeFace); 


      //Quote Name 
      final EditText quoteName = (EditText)layout.findViewById(R.id.quoteNameEditText); 

      layout.findViewById(R.id.cancelButton).setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        //Dismiss the pop up window 
        popupWindow.dismiss(); 
       } 
      }); 

      layout.findViewById(R.id.saveButton).setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        if(!quoteName.getText().toString().equals("")) { 
         Quote quote = new Quote(); 
         quote.setQuoteName(quoteName.getText().toString().trim()); 
         quote.setQuotePercentComplete("0"); 
         quote.setQuoteCreateDate(Helper.getCurrentDate()); 
         DatabaseHandler db = new DatabaseHandler(context); 

         if(!db.isRecordExist(quoteName.getText().toString())) { 
          long insertId = db.insertQuote(quote); 
          db.close(); 
          //Start the activity 
          Intent intent = new Intent(context, NewQuoteTabActivity.class); 
          intent.putExtra("create_new_quote", true); 
          intent.putExtra("quote_name", quoteName.getText().toString()); 
          intent.putExtra("id", insertId); 
          startActivity(intent); 
         } else 
          Toast.makeText(context, getString(R.string.quote_duplicate), Toast.LENGTH_LONG).show(); 
        } else 
         Toast.makeText(context, context.getString(R.string.enter_quote_name), Toast.LENGTH_LONG).show(); 
       } 
      }); 

      //Save Button and Cancel Button 
      ((Button)layout.findViewById(R.id.saveButton)).setTypeface(typeFace); 
      ((Button)layout.findViewById(R.id.cancelButton)).setTypeface(typeFace); 
     } catch(Exception e) { 
      Log.v(GlobalVars.TAG, "Exeption at::" + e.getMessage()); 
     } 
    } 
+0

這種情況PopupWindow,becasue活動被重新啓動(重新)上旋轉的變化。爲了糾正這種情況,當彈出窗口第一次出現時,設置一個全局標誌,或者從任何地方都可以看到的東西(甚至可能是複選框,它被選中等)。然後在'onCreate'方法中,chech如果檢查了這個標誌(如果(true))並重新顯示彈出窗口。 – g00dy

回答

0

我設置了Android target=android-13並在活動

android:configChanges="orientation|screenSize|keyboardHidden"此設置。

它的工作正常。

謝謝。

0

嘗試顯示像

final View parent = findViewById(R.id.{id}); 
parent.post(new Runnable() { 
    @Override 
    public void run() { 
     mPopup.showAtLocation(parent, ...); 
    } 
}); 
相關問題