【问题标题】:Enter and Exit Animations not working in Dialog Fragment进入和退出动画在对话框片段中不起作用
【发布时间】:2017-06-11 16:21:23
【问题描述】:

我已经检查了所有关于此的 Stack Overflow Q/A,仍然找不到解决方案。

这里是文件:

DialogFragment.java

package app.com.thetechnocafe.mealsquickie.Dialogs;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import app.com.thetechnocafe.mealsquickie.R;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by gurleensethi on 26/01/17.
 */

public class NewCategoryDialog extends DialogFragment {

    @BindView(R.id.category_name_text_input_layout)
    TextInputLayout mCategoryNameTextInputLayout;
    @BindView(R.id.category_name_text_input_edit_text)
    TextInputEditText mCategoryNameTextInputEditText;
    @BindView(R.id.cancel_button)
    Button mCancelButton;
    @BindView(R.id.add_button)
    Button mAddButton;

    private OnAddCategoryListener mListener;

    //Interface for callbacks
    public interface OnAddCategoryListener {
        void onCategoryAdded(String category);
    }

    //Instance method
    public static NewCategoryDialog getInstance() {
        return new NewCategoryDialog();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //Inflate the custom dialog
        View root = LayoutInflater.from(getContext()).inflate(R.layout.dialog_new_category, container, false);

        ButterKnife.bind(this, root);

        setEventListeners();

        //Set properties
        getDialog().requestWindowFeature(STYLE_NO_TITLE);
        setCancelable(false);

        return root;
    }

    @Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    private void setEventListeners() {
        mCancelButton.setOnClickListener(view -> getDialog().dismiss());

        mAddButton.setOnClickListener(view -> validateAndSubmitFields());
    }

    private void validateAndSubmitFields() {
        if (mListener != null) {
            //Remove all the already existing errors
            mCategoryNameTextInputLayout.setErrorEnabled(false);

            String category = mCategoryNameTextInputEditText.getText().toString();

            if (category.equals("")) {
                mCategoryNameTextInputLayout.setError(getString(R.string.category_name_cannot_be_empty));
                return;
            }

            mListener.onCategoryAdded(category);
        } else {
            Toast.makeText(getContext(), "No Listener attached for adding new category. Please contact the developer.", Toast.LENGTH_SHORT).show();
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);

        dialog.getWindow().getAttributes().windowAnimations = R.style.DialogSlideFromBottomAnimation;
        return dialog;
    }
}

slide_down.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="1000"
        android:fromYDelta="0%"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toYDelta="100%" />
</set>

slide_up.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="1000"
        android:fromYDelta="100%"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toYDelta="0%" />
</set>

显示对话框:

DialogFragment dialog = NewCategoryDialog.getInstance();
dialog.show(getFragmentManager(), DIALOG_NEW_CATEGORY_TAG);

getAttributes().windowAnimationssetWindowAnimations()我都试过了,也试过把它放在onActivityCreated、onCreateDialog、onCreateView里,但是好像不行。

【问题讨论】:

  • 你找到解决办法了吗?
  • @DrissBounouar 我离开了原生移动开发,我正在做 Web 开发并使用 Flutter 进行移动开发。
  • 发现我的问题,正在停用我设备中的所有动画,我检查了一个新设备,一切都很好。我会将其发布为答案,因此可能会对某人有所帮助。

标签: android


【解决方案1】:

无论您选择哪种解决方案,您都可能遇到与我相同的问题。

在安装新版本之前,我需要从我的开发设备卸载游戏以使更改生效。

我不知道为什么,但我想这与 Android Studio 上的优化部署没有识别更改有关。

【讨论】:

  • 我也刚看到这个,大概花了 2 个小时。谢谢 AS :)
  • 同样,一旦你让 DialogFragment 的动画工作,它就会完全停止工作,直到你重新安装......
【解决方案2】:

试试这个。

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if (getDialog().getWindow() != null) {
            dialog.getWindow().getAttributes().windowAnimations = R.style.DialogSlideFromBottomAnimation;
        }
    }

【讨论】:

    【解决方案3】:

    在您的onStart 的片段中, 执行以下操作:

      // safety check
      if (getDialog() == null) {
        return;
      }
    
      // set the animations to use on showing and hiding the dialog
      getDialog().getWindow().setWindowAnimations(
          R.style.dialog_animation_slide);
    

    上面设置的样式应该这样定义:

    
    <style
      name="dialog_animation_slide" >
      <item name="android:windowEnterAnimation">@anim/slide_up</item>
      <item name="android:windowExitAnimation">@anim/slid_down</item>
    </style>
    
    

    请务必将您的slide_up.xmlslide_down.xml 放在res/anim 目录中

    请从onCreateDialog() 中删除dialog.getWindow().getAttributes().windowAnimations = R.style.DialogSlideFromBottomAnimation;

    让我知道这是否适合你!!!

    【讨论】:

      【解决方案4】:

      如果没有任何效果,可能是设备在开发者选项中禁用了动画。恢复默认选项并检查。

      【讨论】:

        【解决方案5】:

        动画在涉及到 DialogFragment 时有点棘手。

        当您需要更改布局中视图的可见性或位置时,您应该包含细微的动画以帮助用户了解 UI 的变化情况。

        因此,为了实现这一点,请记住 DialogFragmentDialog 类的包装器,您应该为基础 Dialog 得到你想要的动画:

        public class CustomDialogFragment extends DialogFragment
        {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) 
        {
            return super.onCreateView(inflater, container, savedInstanceState);
        }
        
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) 
        {
            // Set a theme on the dialog builder constructor!
            AlertDialog.Builder builder = 
                new AlertDialog.Builder( getActivity(), R.style.MyCustomTheme );
        
            builder  
            .setTitle( "Your title" )
            .setMessage( "Your message" )
            .setPositiveButton( "OK" , new DialogInterface.OnClickListener() 
                {      
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                  dismiss();                  
                }
            });
            return builder.create();
        }
        }
        

        然后您只需要定义包含所需动画的主题即可。在 styles.xml 中添加您的自定义主题:

        slide_down.xml

        <?xml version="1.0" encoding="utf-8"?>
        <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate
            android:duration="1000"
            android:fromYDelta="0%"
            android:interpolator="@android:anim/accelerate_interpolator"
            android:toYDelta="100%" />
        </set>
        

        slide_up.xml

        <?xml version="1.0" encoding="utf-8"?>
        <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate
            android:duration="1000"
            android:fromYDelta="100%"
            android:interpolator="@android:anim/accelerate_interpolator"
            android:toYDelta="0%" />
        </set>
        

        styles.xml

        <style name="MyCustomTheme" parent="@android:style/Theme.Panel">
            <item name="android:windowAnimationStyle">@style/MyAnimation.Window</item>
        </style>
        
        <style name="MyAnimation.Window" parent="@android:style/Animation.Activity"> 
            <item name="android:windowEnterAnimation">@anim/slide_down</item>
            <item name="android:windowExitAnimation">@anim/slide_up</item>
        </style>    
        

        仅此而已……希望对您有所帮助。这要归功于这个答案:Show DialogFragment with animation growing from a point

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多