在工具栏的子元素顶部添加回扣

在工具栏的子元素顶部添加回扣

问题描述:

我有以下布局文件。如何在我的下面的布局中在工具栏的Imageview子项上添加后退按钮(箭头)(或覆盖在z-index上)?在工具栏的子元素顶部添加回扣

<LinearLayout 
android:layout_height="wrap_content" 
android:layout_width="fill_parent" 
android:orientation="vertical" 
xmlns:android="http://schemas.android.com/apk/res/android"> 


<Toolbar 
    android:layout_width="fill_parent" 
    android:layout_height="600dp" 
    android:id="@+id/toolbar" 
    android:background="#313B45" 
    android:weightSum="1" 
    android:contentInsetLeft="0dp" 
    android:contentInsetStart="0dp" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <ImageView 
      android:id="@+id/headerimage" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:scaleType="fitXY" 
      android:layout_gravity="left|top" 
      android:layout_weight="1" /> 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:text="New Text" 
      android:id="@+id/textView" 
      android:scaleType="fitXY" 
      android:layout_gravity="left|top" 
      android:layout_weight="1" /> 

    </LinearLayout> 
</Toolbar> 

+0

你想后退按钮,将带你到以前的活动。 – 2015-04-04 16:03:39

+0

在工具栏的左端? – 2015-04-04 16:03:54

+0

我想要一个出现在ImageView上的按钮(它本身位于工具栏的左上角)。在印刷机上,它应该退出应用程序。 – *N 2015-04-04 16:22:56

你必须设置工具栏上的主页按钮的下面一行getSupportActionBar()使setDisplayHomeAsUpEnabled(真)。并重写后退按钮上的操作,您必须重写onOptionItemSelected方法。寻找给定的代码它会帮助你...:d

 Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setHomeButtonEnabled(true); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

要覆盖OnOptionItemSelected使用下面的代码

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case android.R.id.home: 
     finish(); 
     break; 

    default: 
     break; 
    } 
     return super.onOptionsItemSelected(item); 
    }