我目前正在我的应用中使用 android.support.v7.widget.Toolbar
和 android.support.v4.widget.DrawerLayout
。一切都正常,但我想改变它的行为有一点点。
当我打开抽屉时,整个抽屉占据space of the Toolbar
。如果 Google Music app
,工具栏会保持在最佳状态。我怎样才能做到这一点?
但最重要的不是以前的事情。 At first, the icon which is loaded in the application is the three stripped one
。我意识到打开抽屉后,图标变为箭头。加载片段后,箭头仍然保留在工具栏图标中,即使我按回到第一个屏幕。打开抽屉后,我怎么能避免出现箭头?我想手动更改此图标,特别是当我加载较低级别的片段时。
感谢您的帮助!
代码:
public class HomeActivity extends BaseActivity {
...
private DrawerLayout mDrawer;
private ActionBarDrawerToggle mDrawerToggle;
private ListView mDrawerList;
private ListView mDrawerRightList;
private RelativeLayout mDrawerRelativeLayout;
private String[] mDrawerMenuTitles;
private Toolbar mToolbar;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDrawerMenuTitles = getResources().getStringArray(R.array.main_menu_options);
mDrawer = (DrawerLayout) findViewById(R.id.drawer);
mDrawerRelativeLayout = (RelativeLayout) findViewById(R.id.theDrawerRelativeLayout);
mDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerRightList = (ListView) findViewById(R.id.theDrawerRight);
mDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, mDrawerRightList);
mDrawerList = (ListView) findViewById(R.id.theDrawer);
mDrawerList.setAdapter(new ArrayAdapter<String>(
getSupportActionBar().getThemedContext(),
R.layout.drawer_list_item,
mDrawerMenuTitles
));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
setmToolbar((Toolbar) findViewById(R.id.toolbar));
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawer,
mToolbar,
R.string.drawer_open,
R.string.drawer_close){
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
// TODO Auto-generated method stub
super.onDrawerSlide(drawerView, slideOffset);
}
@Override
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
//getSupportActionBar().setTitle(CURRENT_FRAGMENT);
}
/** Called when a drawer has settled in a completely open state. */
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
//getSupportActionBar().setTitle("Configuración");
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerToggle.syncState();
mDrawer.setDrawerListener(mDrawerToggle);
setToolbarSubtitle(getString(R.string.misrutas_titulo));
initialisePreferences(savedInstanceState);
if (savedInstanceState == null) {
replaceFragment(DEFAULT_FRAGMENT);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mDrawer.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
private class DrawerItemClickListener implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//selectItem(position);
final int thePos = position;
mDrawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
@Override
public void onDrawerClosed(View drawerView){
....
}
});
if(mDrawer.isDrawerOpen(GravityCompat.START))
mDrawer.closeDrawer(mDrawerRelativeLayout);
}
}
@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 toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public void onBackPressed(){
setToolbarSubtitle(getString(R.string.app_name_subtitle));
if (getSupportFragmentManager().getBackStackEntryCount() == 1){
finish();
}
else {
super.onBackPressed();
}
}
private void replaceFragment (String to){
if(!to.equalsIgnoreCase(CURRENT_FRAGMENT)){
CURRENT_FRAGMENT = to;
Fragment fragment = Fragment.instantiate(HomeActivity.this, to);
String backStateName = fragment.getClass().getName();
String fragmentTag = backStateName;
FragmentManager manager = getSupportFragmentManager();
boolean fragmentPopped = manager.popBackStackImmediate (backStateName, 0);
if (!fragmentPopped && manager.findFragmentByTag(fragmentTag) == null){ //fragment not in back stack, create it.
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.content_frame, fragment, fragmentTag);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(backStateName);
ft.commit();
}
}
}
//More methods
....
}
修改
我能够稍微修改一下我的布局,以便工具栏保持在其余屏幕(抽屉和片段)的顶部。但我无法控制图标。当我打开抽屉时,汉堡包图标会转换为箭头。当我关闭抽屉时,箭头转换成汉堡包。但是当我按下抽屉里的一个选项时,一个新的片段会膨胀,汉堡包图标会被箭头取代,直到应用程序关闭,这样就不会看到汉堡包图标了。
当我点击抽屉选项时,如何调整图标行为以使其不会从汉堡变为箭头?
答案 0 :(得分:0)
现在我尝试做一个类似的导航抽屉... 基本上尊重我的是,我有一个不同的导航抽屉包,我认为这与你的导航抽屉不同......我的包裹是:
//From android studio
compile 'com.android.support:appcompat-v7:21.0.0'
我不知道这是否可以帮助你,但我遵循这个最棒的教程,我的导航抽屉的工作方式与你想要的相似!
http://androideity.com/2013/12/16/android-navigation-drawer-parte-1/ http://androideity.com/2014/02/26/android-navigation-drawer-parte-2/
我等着我帮助你!祝你好运!
PD:如果您对本教程有疑问或给我相同的建议! :d答案 1 :(得分:0)
乍一看,无论何时选择项目,您都要设置新的抽屉监听器,正好在此片段中:
private class DrawerItemClickListener implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//selectItem(position);
final int thePos = position;
mDrawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
@Override
public void onDrawerClosed(View drawerView){
....
}
});
if(mDrawer.isDrawerOpen(GravityCompat.START))
mDrawer.closeDrawer(mDrawerRelativeLayout);
}
}
此代码段替换了ActionBarDrawerToggle
(也实现了DrawerListener
),因此不会对控制图标的ActionBarDrawerToggle进行调用。
How can I adapt the icon behaviour so that it doesn't change from hamburger to arrow when I click over a drawer option?
关于你之前的陈述,这听起来对我来说很模糊。为什么你要阻止这种默认行为?您可以通过DrawerListener
的方法调用超级电话来防止变形。