2014-03-05 21 views
2

我可以在actionbar中添加自定義視圖。我的應用程序支持2.3版本,所以我使用ActionbarActivity。現在我的查詢是從actionbar中刪除應用程序圖標,只有回家的圖標,即後面的圖像。刪除帶自定義視圖的操作欄圖標並顯示主頁向上按鈕

我嘗試了很多在搜索時發現的選項。但它不適用於自定義視圖。

編輯

ActionBar actionBar = getSupportActionBar(); 
    LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, 
      LayoutParams.WRAP_CONTENT, Gravity.RIGHT 
        | Gravity.CENTER_VERTICAL); 
    View customNav = LayoutInflater.from(this).inflate(
      R.layout.custom_action_view, null); // layout which contains 

    actionBar.setCustomView(customNav, lp); 


    actionBar.setDisplayHomeAsUpEnabled(true); 
    actionBar.setDisplayShowHomeEnabled(true); 
    actionBar.setDisplayShowCustomEnabled(true); 

    actionBar.setBackgroundDrawable(new ColorDrawable(Color 
      .parseColor("#a40404"))); 

幫我出這個問題。提前致謝。

+2

沒有回家圖標無法顯示按鈕。 –

+0

顯示一些代碼.... – Namy

+0

@DoctororDrive是不可能的? – BSKANIA

回答

-1

要刪除Actonbar圖標

只需添加actionBar.setDisplayUseLogoEnabled(false);此行oncreate()方法:

要啓用後退按鈕

你需要擴展SherlockActivity並插入該代碼在oncreate()方法:

getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

使用此代碼回手柄按鈕動作:

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case android.R.id.home: 
       // do your action here when click on back button showing on action bar 
       onBackPressed(); 
       } 
    } 
1

向上按鈕連接到主頁圖標。這就是爲什麼它被稱爲「家爲up」setDisplayHomeAsUpEnabled(true)

所以你不能顯示帶有禁用「家」圖標的箭頭。

但是,您可以在自定義視圖中製作一個看起來像它的視圖。

例如:

<LinearLayout 
     android:id="@android:id/home" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center_vertical" 
     android:orientation="horizontal" 
     android:background="?attr/actionBarItemBackground" 
     android:paddingRight="5dp"> 

     <!-- this will show tha up arrow --> 
     <ImageView 
      android:id="@+id/up" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:src="?attr/homeAsUpIndicator"/> 

     <!-- place here any View that will be clickable along with "up" arrow (in this example, it's an icon) --> 
     <ImageView 
      android:id="@+id/home_icon" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_marginTop="@dimen/action_bar_icon_vertical_padding" 
      android:layout_marginBottom="@dimen/action_bar_icon_vertical_padding" 
      android:adjustViewBounds="true" 
      android:scaleType="fitCenter" 
      android:src="@drawable/app_icon"/> 

    </LinearLayout> 

android:src="?attr/homeAsUpIndicator" 

設置會根據您的主題向上箭頭。

android:background="?attr/actionBarItemBackground" 

將設置藍色選擇背景按下狀態

現在設置一個OnClickListener到佈局箭頭

View customNav = LayoutInflater.from(this).inflate(
     R.layout.custom_action_view, null); 

customNav.findViewById(android.R.id.home).setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick() { 
     // up button clicked 
    } 
}); 


actionBar.setCustomView(customNav, lp);