如何为我的所有活动创建自定义标题栏?
我见过很多创建自定义标题栏的示例: http://zaman91.wordpress.com/2010/03/16/android-how-to-add-custom-title-bar/
有更简单的方法吗?我认为必须在 onCreate() 中为每个活动实现自定义标题栏代码有点矫枉过正。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不使用包含?将标准标题栏的设计放入布局 XML 文件中,然后将其包含在每个活动布局中:
然后创建 my_title_bar.xml 以包含栏的内容。
如果标题栏中有可点击的视图,则需要一些代码,但您可以子类化 Activity 来提供点击方法。
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:
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.
创建一个 Activity 基类并编写代码来绘制自定义标题栏。由此派生您的活动。如果您希望更改标题栏的标题文本,您还可以在 Activity 基类中编写一个 setter 方法。
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.