How to align title at center of ActionBar in default theme(Theme.Holo.Light)

后端 未结 12 2192
野性不改
野性不改 2020-11-27 12:08

I searched a lot but can not find a way, How can I set then title at center of ActionBar instead of left aligned. I used below code to set the title at center :



        
12条回答
  •  佛祖请我去吃肉
    2020-11-27 12:41

    You can create a custom layout and apply it to the actionBar.

    To do so, follow those 2 simple steps:

    1. Java Code

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

    Where R.layout.actionbar is the following layout.

    1. XML

      
      
      
      
      
      

    It can be as complex as you want. Try it out!

    EDIT:

    To set the background you can use the property android:background in the container layout (LinearLayout in that case). You may need to set the layout height android:layout_height to match_parent instead of wrap_content.

    Moreover, you can also add a LOGO / ICON to it. To do so, simply add an ImageView inside your layout, and set layout orientation property android:orientation to horizontal (or simply use a RelativeLayout and manage it by yourself).

    To change the title of above custom action bar dynamically, do this:

    TextView title=(TextView)findViewById(getResources().getIdentifier("action_bar_title", "id", getPackageName()));
    title.setText("Your Text Here");
    

提交回复
热议问题