如何在ActionBar AppCompatActivity Android Studio中创建后退按钮

时间:2016-11-04 09:22:40

标签: java android-studio

我创建了一个简单的Navigation Drawer ActivityBasic Activity,我将其命名为LoginActivity,所以当我从抽屉中选择一个项目时,我能够导航到我的LoginActivity,但是没有BackButton在我的ActionBar中,我来自Xamarin Forms,在那里,根据导航类型,后退按钮是可见的!到目前为止,我有:

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar); ................

public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        Intent intent = new Intent(this,LoginActivity.class);
        startActivity(intent);

Android Studio 2.2.2
compileSdkVersion 24

1 个答案:

答案 0 :(得分:0)

在onCreate方法中更改此代码:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.YOUR_NAVIGATION_ICON); //This is the line to add

此外,您可以确定点击此图标的功能:

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case android.R.id.home:
                //Whatever your require the navigation to do
                return true;

        }
    }