弹出窗口在屏幕方向上销毁

时间:2013-08-12 08:08:58

标签: android

我正在使用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());
        }
    }

2 个答案:

答案 0 :(得分:0)

我设置了Android target=android-13并在活动

中设置了它

android:configChanges="orientation|screenSize|keyboardHidden"

工作正常。

感谢。

答案 1 :(得分:0)

尝试像

一样显示PopupWindow
final View parent = findViewById(R.id.{id});
parent.post(new Runnable() {
    @Override
    public void run() {
        mPopup.showAtLocation(parent, ...);
    }
});