How To Center Icon in Android Action Bar

前端 未结 3 2065
故里飘歌
故里飘歌 2021-01-04 08:25

I\'m trying to center the icon in the android action bar. I\'m using a custom layout that has a button on the left side, an icon in the center and the menu options on the r

3条回答
  •  再見小時候
    2021-01-04 08:41

    The previous answers doesn't work for me (on Android 4.4 devices), because the outer container view groups did not expand to the full with, so the container group and the centered inner logo was already left aligned in the title bar.

    I found a layout that works with the regular action bar menu (on right side) and regardless of enabling the regular action bar logo and title (on left side). This example only centers an additional logo.

    
    
    
        
    
        
    
        
    
    
    

    You have to enable the custom view by this

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayShowCustomEnabled(true);
    View cView = getLayoutInflater().inflate(R.layout.actionbar, null);
    actionBar.setCustomView(cView);
    

    but not this method (it disables all other elements in action bar).

    // actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    

提交回复
热议问题