尝试调暗活动背景时,Android getWindow()未定义

时间:2014-12-26 08:38:26

标签: java android

在尝试在没有对话框的情况下调暗Activity的背景几秒钟时,我遇到了一些问题。

所以我要做的就是当点击按钮时,它会使背景变暗几秒钟。然后在同一时间,我的另一个意图是加载内容。内容完成加载后,它将转移到另一个页面。这是代码:

ivTwitterShare.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            WindowManager.LayoutParams windowManager = getWindow().getAttributes();
            windowManager.dimAmount = 0.75f;
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            // Create intent using ACTION_VIEW and a normal Twitter url
            tweetUrl = String
                    .format("",
                            urlEncode("Come join us for "
                                    + txtEventDtlName.getText().toString()),
                            urlEncode("at "
                                    + txtEventDtlAddr.getText().toString()
                                    + " on "
                                    + txtEventDtlDate.getText().toString()));

            Thread newThread = new Thread() {
                @Override
                public void run() {
                    try {
                        super.run();
                        sleep(10000);
                    } catch (Exception e) {

                    } finally {
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri
                                .parse(tweetUrl));
                        // Narrow down to official Twitter app
                                                    startActivity(intent);                      }
                }
            };
            newThread.start();
        }
    });

但是,我在getWindow()收到错误消息:The method getWindow() is undefined for the type new View.OnClickListener(){}

有什么想法吗?提前谢谢。

2 个答案:

答案 0 :(得分:1)

您应该使用YourActivityClassName.this.getWindow()

<强>更新

好的,我知道你只是想调暗后面然后开始一个活动,所以你不需要下面的动画。

问题出现在:

WindowManager.LayoutParams windowManager = getWindow().getAttributes();
windowManager.dimAmount = 0.75f;

你应该将windowManager压入Window的attr。它与所有LayoutParams相同。

WindowManager.LayoutParams windowManager = getWindow().getAttributes();
windowManager.dimAmount = 0.75f;
getWindow().setAttributes(windowManager);

以下是setAttributes的源代码:

    /**
 * Specify custom window attributes.  <strong>PLEASE NOTE:</strong> the
 * layout params you give here should generally be from values previously
 * retrieved with {@link #getAttributes()}; you probably do not want to
 * blindly create and apply your own, since this will blow away any values
 * set by the framework that you are not interested in.
 *
 * @param a The new window attributes, which will completely override any
 *          current values.
 */
public void setAttributes(WindowManager.LayoutParams a) {
    mWindowAttributes.copyFrom(a);
    if (mCallback != null) {
        mCallback.onWindowAttributesChanged(mWindowAttributes);
    }
}

它显示您需要调用它来告诉视图更改。

------------------------------------- old answer -------- ---------

使用xml定义对话框的动画(share_fade_in.xml):

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">

    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration="200"
    />
</set>

这是淡入淡出,对于淡出,您应该交换android:fromAlphaandroid:toAlpha的值。

在style.xml中为对话框定义样式

<style name="AnimBottom" parent="@android:style/Animation">
    <item name="android:windowEnterAnimation">@anim/share_fade_in</item>
    <item name="android:windowExitAnimation">@anim/share_fade_out</item>
</style>

然后应用到您的对话框:

this.getWindow().setWindowAnimations(R.style.AnimBottom);

答案 1 :(得分:0)

getWindow().setBackgroundDrawable(new ColorDrawable(Color.argb(150, 0, 0, 0)));

昏暗的活动如上所述。