The first step is to customize the title bar to be written in the oncreate method.
Requestwindowfeature (window. feature_custom_title );
- Setcontentview (R. layout. Main );
- Getwindow (). setfeatureint (window. feature_custom_title, R. layout. title_bar );
Note:
Requestwindowfeature must be before setcontentview
Getwindow (). setfeatureinit is better after setcontentview
Step 2: write your own layout file to customize the title bar.
However, we may encounter some problems, that is, the height of the title bar cannot be customized ~ The following is the solution ~
Next, let's take a look at the XML file of the title bar of Android in the source framework.
Screen_custom_title.xml
<Linearlayout Android: Orientation = "vertical" Android: fitssystemwindows = "true">
- <Framelayout Android: Id = "@ Android: ID/title_container"
- Android: layout_width = "match_parent"
- Android: layout_height = "? Android: ATTR/windowtitlesize"
- Style = "? Android: ATTR/windowtitlebackgroundstyle ">
- </Framelayout>
- <Framelayout Android: Id = "@ Android: ID/content"
- Android: layout_width = "match_parent"
- Android: layout_height = "0dip"
- Android: layout_weight = "1"
- Android: foregroundgravity = "fill_horizontal | top"
- Android: foreground = "? Android: ATTR/windowcontentoverlay "/>
- </Linearlayout>
Screen_title.xml
<Linearlayout Android: Orientation = "vertical" Android: fitssystemwindows = "true">
- −
- <Framelayout Android: layout_width = "match_parent"
- Android: layout_height = "? Android: ATTR/windowtitlesize"
- Style = "? Android: ATTR/windowtitlebackgroundstyle ">
- <Textview Android: Id = "@ Android: ID/Title"
- Style = "? Android: ATTR/windowtitlestyle"
- Android: Background = "@ null"
- Android: fadingedge = "horizontal"
- Android: gravity = "center_vertical"
- Android: layout_width = "match_parent"
- Android: layout_height = "match_parent"/>
- </Framelayout>
- <Framelayout Android: Id = "@ Android: ID/content"
- Android: layout_width = "match_parent"
- Android: layout_height = "0dip"
- Android: layout_weight = "1"
- Android: foregroundgravity = "fill_horizontal | top"
- Android: foreground = "? Android: ATTR/windowcontentoverlay "/>
- </Linearlayout>
Since it is a custom title, let's take a look at screen_custom_title.xml, which contains a title_container and a content, which are combined into a title bar. The view given by the custom title, content is used as a sub-view, which cannot affect the title_container and content. Therefore, if you want to get it, it should be left blank, but you have no choice.
Check that title_container has a style like this:
Style = "? Android: ATTR/windowtitlebackgroundstyle"
Content's foreground is Android: foreground = "? Android: ATTR/windowcontentoverlay"
We can write a style by ourselves.
<Resources xmlns: Android = "http://schemas.android.com/apk/res/android">
- <Style name = "customwindowtitlebackground">
- <Item name = "Android: background"> #00cc00 </item>
- </Style>
- <Style name = "test" parent = "Android: Theme">
- <Item name = "Android: windowtitlesize"> 50dp </item>
- <Item name = "Android: windowtitlebackgroundstyle"> @ style/customwindowtitlebackground </item>
- </Style>
- </Resources>
Add to androidmanifest
<Activity Android: Name = ". customtitilebaractivity"
- Android: Label = "@ string/app_name" Android: theme = "@ style/title_bar">
- <Intent-filter>
- <Action Android: Name = "android. Intent. Action. Main"/>
- <Category Android: Name = "android. Intent. Category. launcher"/>
- </Intent-filter>
- </Activity>