2014-10-29 52 views
6

我有一個應用程序,其中加載了一個動作菜單split action bar如何將工具欄上的動作菜單居中

我改變了動作條用於新的工具欄,並通過在獨立模式下使用的其它工具欄替換分割動作條:

Toolbar toolbarBottom = (Toolbar) findViewById(R.id.toolbarBottom); 
toolbarBottom.inflateMenu(R.menu.ab_one_cam); 

作爲文檔的操作菜單是銷到工具欄的右側規定: enter image description here

,但我想的圖標可以在工具欄上的中心,就像是在分割動作條: enter image description here

如何使動作菜單佔據工具欄上的所有可用空間?

工具欄專用於此菜單,其他任何內容都不會添加。

回答

接受的答案的鏈接導致分裂的工具欄。如果你像我一樣有非常簡單的,需要此代碼是不夠好:

public class SplitToolbar extends Toolbar { 

    public SplitToolbar(Context context) { 
     super(context); 
    } 

    public SplitToolbar(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public SplitToolbar(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @Override 
    public void addView(View child, ViewGroup.LayoutParams params) { 
     if (child instanceof ActionMenuView) { 
      params.width = LayoutParams.MATCH_PARENT; 
     } 
     super.addView(child, params); 
    } 
} 

幸得:https://gist.github.com/dodgex/7bc81fd2cbb70a8d5117

+0

另請參見:http://stackoverflow.com/questions/34546160/how-to-enable-split-action-bar/34546493#34546493 – piotrek1543 2015-12-31 15:06:04

回答

6

在這種情況下,Chris Banes建議使用ActionMenuView而不是Toolbar(請參閱下面的鏈接,回覆#6)。除此之外,在這個鏈接中,你可以找到一個解決方案,在這個解決方案中,分組工具欄爲了分割工作正確。

https://code.google.com/p/android/issues/detail?id=77632#c2

希望它可以幫助你!

+1

在線程的評論中使用詳細的SplitToolbar做的竅門:https://gist.github.com/dodgex/7bc81fd2cbb70a8d5117 – grunk 2014-11-05 08:50:47

1

我也一直在試圖找到一個回答這個問題在過去的幾個星期。我發現的最接近的事物是利用了Toolbar只是一個ViewGroup。您只需要創建一個menu_items佈局,該佈局只是一個LinearLayout,其中等於加權的menu items。我知道這不是一個理想的解決方案,但我還沒有找到在使用默認菜單時分散項目。

<android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar_btm" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:minHeight="?attr/actionBarSize" 
     android:background="@color/toolbar_bkgnd" 
     android:layout_alignParentBottom="true" 
     app:theme="@style/ThemeOverlay.AppCompat.ActionBar" > 

     <include layout="@layout/menu_items"/> 

     </android.support.v7.widget.Toolbar> 
+1

您是否仍然可以在此解決方案中使用inflateMenu()和setOnMenuItemClickListener()? – grunk 2014-10-31 14:29:30

+0

是的,沒有。如果您使用的是溢出菜單,則仍然可以使用默認方法來處理其他菜單項,您需要通過XML設置佈局並自己監聽onClick事件。 – MrEngineer13 2014-10-31 14:57:39

+0

沒有理由使用工具欄然後,更好地創建一個自定義視圖:( – grunk 2014-10-31 15:30:38