简体   繁体   English

如何为我的所有活动创建自定义标题栏?

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

I have seen many examples of creating a custom titlebar: http://zaman91.wordpress.com/2010/03/16/android-how-to-add-custom-title-bar/ 我已经看到了许多创建自定义标题栏的示例: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. 我认为必须为每个活动在onCreate()中实现自定义标题栏代码有点过大。

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: 将标准标题栏的设计放入布局XML文件中,然后将其包含在每个活动布局中:

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

And then create my_title_bar.xml to contain the contents of the bar. 然后创建my_title_bar.xml来包含栏的内容。

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子类化以提供click方法。

Create an Activity base class and write the code to draw the custom title bar. 创建一个Activity基类,并编写代码以绘制自定义标题栏。 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. 如果要更改标题栏的标题文本,则还可以在Activity基类中编写一个setter方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM