2015-04-04 36 views
3

我有以下佈局文件。如何在我的下面的佈局中在工具欄的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上的按鈕(它本身位於工具欄的左上角)。在印刷機上,它應該退出應用程序。 – stackoverflowN 2015-04-04 16:22:56

回答

3

你必須設置工具欄上的主頁按鈕的下面一行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); 
    }