Change custom actionbar title

淺唱寂寞╮ 提交于 2019-12-10 15:14:59

问题


I'm using a custom layout to center the title of the actiobar.

  ActionBar actionBar = getActionBar(); 
  actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
  actionBar.setCustomView(R.layout.action_bar);

where action_bar is a simple linear layout with a textview

My problem is that I need every activity to have different names - and I need to change theme programmatically. So I'm trying something like this:

private void setActiobarTitle(String title)
{
   LayoutInflater inflator  = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   View v = inflator.inflate(R.layout.action_bar, null);
   TextView titleTxtView = (TextView) v.findViewById(R.id.actionbarTitle);
   titleTxtView.setText(title);
}

When debuging the textview is found and initialized and everything seems to be okay - but the title doesn't change. Can anyone please tell me what seems to be the problem?


回答1:


Modify the setActionbarTitle() as below

 private void setActiobarTitle(String title)
 {

    View v = getActionbar().getCustomView();
    TextView titleTxtView = (TextView) v.findViewById(R.id.actionbarTitle);
    titleTxtView.setText(title);
 }


来源:https://stackoverflow.com/questions/20091791/change-custom-actionbar-title

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!