How to make android action bar title center aligned?

前端 未结 2 1732
借酒劲吻你
借酒劲吻你 2020-12-09 19:26

I tried my own Custom Action bar for getting action bar title centered on the middle. It worked. However, after I added the options menu in Action bar, the title again shift

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-09 19:57

    I am sure, you are using the below to set your custom theme.

    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
    getSupportActionBar().setCustomView(R.layout.abs_layout);
    

    If not, then please add the first line. And the text in your custom layout is centered. (Gravity = Center)

    Also I found a similar link here and here. Hope this helps.

    Try this out as well.

    I was able to accomplish what I wanted by not showing the title of the activity, and instead using a customView as you suggested. The key that I missed for a while was you must use the setCustomView(View, ActionVar.LayoutParams) method in order to get the custom view centered, simply setting a layout_gravity="center" in the custom view layout file does not work. Here's a short code snippet showing how I got it to work:

                TextView customView = (TextView)
    LayoutInflater.from(this).inflate(R.layout.actionbar_custom_title_view_centered,
    null);
                ActionBar.LayoutParams params = new
    ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
    ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
    
                customView.setText("Some centered text");
                getSupportActionBar().setCustomView(customView, params);
    

    This method is working for me on at least 3.2, 2.3, and 2.2 devices.

    Source https://groups.google.com/forum/#!msg/actionbarsherlock/A9Ponma6KKI/Llsp41jWLEgJ

提交回复
热议问题