【发布时间】:2023-03-13 22:14:01
【问题描述】:
我想使用 Google 的动画在打开导航栏时将导航图标变为箭头,然后将其变为抽屉式导航图标。我还想知道在移动到另一个活动时是否可以保持操作栏静止。
---编辑---
这个问题已经过时并且不再相关,因为 Material Design 中的导航抽屉已经在一个简单而无缝的过程中为开发人员提供,不再需要开发人员将汉堡侧菜单添加到后退箭头动画中,因为它已经是提供。
menutitles = getResources().getStringArray(R.array.sidebar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.slider_list);
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.list_tem, R.id.title, menutitles));
// Set the list's click listener
mTitle = mDrawerTitle = getTitle();
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.mipmap.drawer, R.string.drawer_open, R.string.drawer_close) {
/**
* Called when a drawer has settled in a completely closed state.
*/
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/**
* Called when a drawer has settled in a completely open state.
*/
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
/** * When using the ActionBarDrawerToggle, you must call it during * onPostCreate() and onConfigurationChanged()... */
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState(); }
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
mDrawerToggle.onConfigurationChanged(newConfig);
}
/** Swaps fragments in the main content view */
private void selectItem(int position) {
switch (position){
case 0:
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
break;
case 1: Intent intent2 = new Intent(this, Time.class);
startActivity(intent2);
finish();
break;
case 2: Intent intent3 = new Intent(this, Temperature.class);
startActivity(intent3);
finish();
break;
case 3: Intent intent4 = new Intent(this, Weight.class);
startActivity(intent4);
finish();
break;
case 4: Intent intent5 = new Intent(this, Length.class);
startActivity(intent5);
finish();
break;
case 5: Intent intent6 = new Intent(this, Money.class);
startActivity(intent6);
finish();
break;
case 6:
Intent intent1 = new Intent(this, Metric.class);
startActivity(intent1);
finish();
break;
case 7:
Intent intent7 = new Intent(this, Food.class);
startActivity(intent7);
finish();
break;
case 8:
Intent intent8 = new Intent(this, Data.class);
startActivity(intent8);
finish();
break;
}
// Create a new fragment and specify the planet to show based on position
// Highlight the selected item, update the title, and close the drawer
mDrawerList.setItemChecked(position, true);
setTitle("Converjz");
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
selectItem(position);
}
}
}
【问题讨论】:
-
到底是什么问题?
-
好吧,我一直在研究适用于 Android 的 Material Design,其中导航抽屉可以通过动画更改为后退箭头。我找不到这个问题的直接答案,所以我个人询问如何将它添加到我上面的代码中。代码没有问题,我想轻松地将其添加到我的代码中。
-
您正在使用
ActionBarDrawerToggle的v4 支持版本。如果你想要汉堡箭头动画,你需要使用v7 appcompat version。 -
好的,有没有简单的方法来编辑这个对版本 7 的更改?
标签: android animation navigation-drawer