2017-03-17 228 views
0

我已经写了一段代码作为应用程序的一部分,我希望在操作栏/工具栏上实现后退按钮,以便当按下按钮时,将显示前一页(即当前页面/片段之前的页面/片段)。如何将后退按钮添加到操作栏/工具栏

这是工具栏,DrawerLayout,NavigationView和getSupportActionBar()的代码:

final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 

android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 
toolbar.setVisibility(View.VISIBLE); 
getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
getSupportActionBar().setLogo(R.mipmap.ic_launcher); 
getSupportActionBar().setDisplayUseLogoEnabled(true); 
getSupportActionBar().setHomeButtonEnabled(true); 

我无法用动作条。出于某种原因(我不知道为什么),我的Android工作室/程序不会允许我使用ActionBar。所以我用set/getSupportActionBar()来代替它。

用于涉及此功能是:

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.menu_settings, menu); 
    return true; 
}   

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    // 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(); 

    switch (id) { 
     case android.R.id.home: 
      onBackPressed(); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
     } 
    }   

@Override 
public void onBackPressed() { 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    if (drawer.isDrawerOpen(GravityCompat.START)) { 
     drawer.closeDrawer(GravityCompat.START); 
    } else { 
     super.onBackPressed();   
    } 
} 

我activity_main.xml中的文件是:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    android:id="@+id/activity_main" 
    android:orientation="vertical" 
    tools:openDrawer="start" 
    tools:context="com.example.albin.settings_menu.SettingsActivity"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#fff"> 

    <android.support.v7.widget.Toolbar 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
      app:title="Settings"/> 

    <android.support.v4.widget.DrawerLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:id="@+id/drawer_layout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

     <FrameLayout 
     android:id="@+id/frame" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/toolbar"> 

     </FrameLayout> 

      <android.support.design.widget.NavigationView 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:app="http://schemas.android.com/apk/res-auto" 
       android:id="@+id/nav_view" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:layout_gravity="start" 
       android:fitsSystemWindows="true" 
       android:layout_marginTop="-24dp" 
       app:menu="@menu/options_menu" /> 


     </android.support.v4.widget.DrawerLayout> 

    </RelativeLayout> 
    </LinearLayout> 

的问题是,我不知道这是有用的代码,其是无用的代码,以及如何混合/连接/(为其添加代码)这些(代码,方法,变量/对象,片段,xml布局)以获得期望的结果,也就是在动作上应用后退按钮酒吧/工具栏。

上面的大部分代码都是针对向上按钮实现的,而不是后退按钮。我在几个地方看过,上下按钮不一样。

我尝试了几个在互联网上的链接以及在这个网站上的链接,但没有一个只是我所需要的。

希望有人可以给我一个明确的答复......

回答

0

最简单的方法是添加清单文件父活动为开发者文档建议。

<activity 
    android:name=".ChildActivity" 
    android:parentActivityName=".ParentActivity" > 

和Java代码,你已经做到了,setSupportActionbar和setHomeAsUpEnabled。

编辑: 其必要添加了行动图标是可见的,如 Android Developer Docs

+1

这是不正确的,至少如果您使用CollapsingToolbarLayout。 在清单中添加android:parentActivityName =“activityParentName”时不显示箭头后退按钮,但是如果添加getSupportActionBar()。setDisplayHomeAsUpEnabled(true); getSupportActionBar()。setDisplayShowHomeEnabled(true);出现 –

+1

@EmmanuelGuther谢谢你的信息,你的正确我编辑了我的答案 – geniushkg

0

所以工具栏可添加灵活地修改标题栏在Android中提及。

至于为什么getActionBar不工作,你不得不使用getSupportActionBar是因为你必须使用SupportLibrary。 SupportLibrary提供了对早期SDK版本的向后兼容性。

如果要广泛修改标题栏/标题/动作栏 ,请使用工具栏,否则使用动作栏。

0

添加导航点击收听到工具栏,如下图所示

toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      onBackPressed(); 
     } 
    }); 
2

实际上有这个问题,因为你已经在RelativeLayout所以抽屉布局上是重叠的补充工具栏布局就是为什么你会不能够点击后退箭头,我有解决您的布局参见下面

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    android:orientation="vertical"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="?attr/colorPrimary" 
     app:navigationIcon="@drawable/ic_back_black" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     app:title="Settings" /> 

    <android.support.v4.widget.DrawerLayout 
     android:id="@+id/drawer_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <FrameLayout 
      android:id="@+id/frame" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/toolbar" /> 

     <android.support.design.widget.NavigationView 
      android:id="@+id/nav_view" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      android:fitsSystemWindows="true" 
      app:menu="@menu/options_menu" /> 
    </android.support.v4.widget.DrawerLayout> 
</LinearLayout> 
4

您可以在ToolBar后退图标:

初始化ToolBar

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 
  • 您可以使用drawable图标为一个后退按钮。

    toolbar.setNavigationIcon(R.drawable.your_drawable_icon); 
    
    toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         // what do you want here 
        } 
    }); 
    
  • 如果你不想使用drawable图标,然后:

    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    getSupportActionBar().setDisplayShowHomeEnabled(true); 
    
    toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         // what do you want here 
        } 
    }); 
    
0

如果引用的操作栏中的一些动作,比如保存操作或共用一个,并且您优先于onOptionsItemSelected方法,那么您需要定义当返回主页按钮被点击时的行为:

@Override public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.action_save: 
     //save stuff 
     break; 

     //this is what you need to add to reference again back/home button 
     case android.R.id.home: 
     //do your stuff here, usually back to the home or close the current activity 
     getActivity().finish(); 
     break; 

     default: 
     break; 
    } 
    return true; 
相关问题