在Android工具栏中,当使用动画旋转图标时,它将其位置转移到左侧



当我尝试使用动画旋转工具栏上的图标时,它开始旋转,但它正在向左转移。

以下是我用于使图标开始旋转并停止旋转的代码

private MenuItem refreshItem;
    private Animation animRotate;
    private ImageButton imgRotate;

refreshitem =菜单。finditem(r.id.action_btn_refresh);

public void refresh() {
            flag = 1;
        if (imgRotate == null) {
            /* Attach a rotating ImageView to the refresh item as an ActionView */
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            imgRotate = (ImageButton ) inflater.inflate(
                    R.layout.view_action_refresh, null);
            // define the animation for rotation
            animRotate = AnimationUtils.loadAnimation(mActivity, anim.rotate);
            animRotate.setRepeatCount(Animation.INFINITE);
        }
        imgRotate.startAnimation(animRotate);
        refreshItem.setActionView(imgRotate);
    }
    public void completeRefresh() {
        flag = 0;
        if (refreshItem != null) {
            refreshItem.getActionView().clearAnimation();
            refreshItem.setActionView(null);
        }
    }

任何人都可以建议我是否需要更改任何东西以停止旋转时的图标转移。

可能是由您的imgRotate的不正确布局引起的,请查看是否有帮助:

<?xml version="1.0" encoding="utf-8"?>
    <ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/menu_animation"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="@*android:dimen/action_button_min_width"
    android:padding="8dp"
    style="@style/Widget.AppCompat.ActionBar"
    tools:ignore="PrivateResource" />

最新更新