How can I create a custom titlebar for all of my activities?

一个人想着一个人 提交于 2019-12-24 10:00:39

问题


I have seen many examples of creating a custom titlebar: http://zaman91.wordpress.com/2010/03/16/android-how-to-add-custom-title-bar/

Is there a simpler way of doing this? I think it is a bit overkill to have to implement the custom titlebar code in onCreate() for every activity.


回答1:


Why not use an include? Put the design of your standard titlebar into a layout XML file, and then include it in each activity layout:

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

And then create my_title_bar.xml to contain the contents of the bar.

If you have clickable views in the title bar you'll need a little code, but you could subclass Activity to provide click methods.




回答2:


Create an Activity base class and write the code to draw the custom title bar. Derive your Activities from this. If you want the title text of your title bar to change, you could also write a setter method in your Activity base class.



来源:https://stackoverflow.com/questions/4249783/how-can-i-create-a-custom-titlebar-for-all-of-my-activities

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