与此处相同的问题:Refresh menu item animation in ActionBarSherlock
除了我想在API 11中使用ActionBarActivity和AppCompat v21 Material Design
执行此操作refresh.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/ic_menu_refresh"
android:background="#F00"/>
MyFragment.java
private View mRefresh;
private MenuItem mRefreshItem;
private boolean mIsRefreshing;
private Animation mRotate;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mRotate = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate);
mRotate.setRepeatCount(Animation.INFINITE);
mRotate.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
if(!mIsRefreshing){
mRefreshItem.getActionView().clearAnimation();
mRefreshItem.setActionView(null);
}
}
});
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mRefresh = inflater.inflate(R.layout.refresh, null);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu, menu);
mRefreshItem = menu.findItem(R.id.action_refresh);
if(mIsRefreshing){
mRefreshItem.setActionView(mRefresh);
mRefresh.startAnimation(mRotate);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.action_refresh:
if(!mIsRefreshing) {
mRefreshItem.setActionView(mRefresh);
mRefresh.startAnimation(mRotate);
startTask();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
动作视图未与静态图标对齐(红色bg,因此我可以看到视图):
答案 0 :(得分:0)
将填充设置为0:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/ic_menu_refresh"
android:padding="0dp"
style="@style/Widget.AppCompat.ActionButton"/>