首页 > 解决方案 > PopupWindow 动画在 Android Studio 4.2.1 中不起作用

问题描述

我想要的是从下到上出现的弹出窗口
res/anim/appear.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <scale
        android:duration="1000"
        android:fillAfter="false"
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="90%"
        android:toXScale="1.0"
        android:toYScale="1.0" />
</set>

res/anim/disapper.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <scale
        android:duration="1000"
        android:fillAfter="false"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="90%"
        android:toXScale="0.0"
        android:toYScale="0.0" />
</set>

res/values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AnimationPopup">
        <item name="android:windowEnterAnimation">@anim/appear</item>
        <item name="android:windowExitAnimation">@anim/disappear</item>
    </style>
</resources>

MainActivity.java。
popWindow.setAnimationStyle(R.style.AnimationPopup);
似乎不起作用。
我已经搜索了互联网,但仍然找不到解决方案。

public class MainActivity extends AppCompatActivity {
    private Button btn_show;
    private MainActivity mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mContext = MainActivity.this;
        btn_show = (Button) findViewById(R.id.btn_show);
        btn_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initPopWindow(v);
            }
        });
    }

    private void initPopWindow(View v) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_popup, null, false);
        TextView Rename = (TextView) view.findViewById(R.id.Rename);
        TextView SaveTo = (TextView) view.findViewById(R.id.SaveTo);

        final PopupWindow popWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popWindow.setAnimationStyle(R.style.AnimationPopup);
        popWindow.showAsDropDown(v, 200, 0);
}

标签: android-layoutandroid-popupwindow

解决方案


推荐阅读