我在用着AppCompat
and Toolbar
.
我确保当导航抽屉图标从汉堡包转变为箭头时会有动画,反之亦然。
我使用以下技术https://stackoverflow.com/a/26469738/72437 https://stackoverflow.com/a/26469738/72437
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">@android:color/white</item>
</style>
</resources>
当我按下导航抽屉滑入和滑出菜单时,会发生动画。然而,它并不是很有用。根据材料设计指南,导航抽屉片段应为全高。因此,当导航抽屉图标开始滑出时,它将阻止它。
在处理我的搜索按钮时,我更感兴趣的是执行动画(action_search
),它充当我的工具栏的操作视图。
主菜单.xml
<item android:id="@+id/action_search"
android:title="Search me"
android:icon="@drawable/ic_add_white_24dp"
app:showAsAction="always|collapseActionView"
app:actionLayout="@layout/collapsible_searchtext" />
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
collapsible_searchtext.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
>
<android.widget.AutoCompleteTextView
android:id="@+id/search"
android:dropDownWidth="match_parent"
android:completionThreshold="1"
android:inputType="textNoSuggestions"
android:imeOptions="actionSearch|flagNoExtractUi"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:hint="Search stock"
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
- 当我按下
action_search
,搜索图标将扩展为AutoCompleteTextView
. 同时,汉堡图标将动画为箭头图标
- 当我按箭头图标时,箭头图标将动画回到汉堡图标。
我尝试通过阅读后使用以下代码来实现目标1https://stackoverflow.com/a/26480439/72437 https://stackoverflow.com/a/26480439/72437
private void animateHamburgerToArrow() {
ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float slideOffset = (Float) valueAnimator.getAnimatedValue();
actionBarDrawerToggle.onDrawerSlide(drawerLayout, slideOffset);
}
});
anim.setInterpolator(new DecelerateInterpolator());
// You can change this duration to more closely match that of the default animation.
anim.setDuration(500);
anim.start();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
animateHamburgerToArrow();
return true;
} else if (id == R.id.action_search) {
animateHamburgerToArrow();
return true;
}
return super.onOptionsItemSelected(item);
}
当我按下action_search
,汉堡图标确实更改为箭头图标。然而,没有动画。我可以知道我错过了什么吗?
Update
我怀疑导航抽屉使用的左箭头与搜索操作视图使用的左箭头不同。它们是两个不同的实例。
这是因为如果我点击action_settings
,我可以观察动画。但是,如果我点击action_search
,我不会看动画。我怀疑搜索操作视图使用的左箭头“阻止”导航抽屉的图标(汉堡包和箭头)