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());
        }
    }
4

2 回答 2

0

我设置了Androidtarget=android-13并在活动中设置了这个

android:configChanges="orientation|screenSize|keyboardHidden".

它工作正常。

谢谢。

于 2013-08-12T08:20:09.647 回答
0

尝试显示 PopupWindow

final View parent = findViewById(R.id.{id});
parent.post(new Runnable() {
    @Override
    public void run() {
        mPopup.showAtLocation(parent, ...);
    }
});
于 2014-08-23T17:07:51.607 回答