Android custom title bar

Source: Internet
Author: User

 

The first step is to customize the title bar to be written in the oncreate method.


Requestwindowfeature (window. feature_custom_title );
  1. Setcontentview (R. layout. Main );
  2. 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">
  1. <Framelayout Android: Id = "@ Android: ID/title_container"
  2. Android: layout_width = "match_parent"
  3. Android: layout_height = "? Android: ATTR/windowtitlesize"
  4. Style = "? Android: ATTR/windowtitlebackgroundstyle ">
  5. </Framelayout>
  6. <Framelayout Android: Id = "@ Android: ID/content"
  7. Android: layout_width = "match_parent"
  8. Android: layout_height = "0dip"
  9. Android: layout_weight = "1"
  10. Android: foregroundgravity = "fill_horizontal | top"
  11. Android: foreground = "? Android: ATTR/windowcontentoverlay "/>
  12. </Linearlayout>

Screen_title.xml

<Linearlayout Android: Orientation = "vertical" Android: fitssystemwindows = "true">
  1. −
  2. <Framelayout Android: layout_width = "match_parent"
  3. Android: layout_height = "? Android: ATTR/windowtitlesize"
  4. Style = "? Android: ATTR/windowtitlebackgroundstyle ">
  5. <Textview Android: Id = "@ Android: ID/Title"
  6. Style = "? Android: ATTR/windowtitlestyle"
  7. Android: Background = "@ null"
  8. Android: fadingedge = "horizontal"
  9. Android: gravity = "center_vertical"
  10. Android: layout_width = "match_parent"
  11. Android: layout_height = "match_parent"/>
  12. </Framelayout>
  13. <Framelayout Android: Id = "@ Android: ID/content"
  14. Android: layout_width = "match_parent"
  15. Android: layout_height = "0dip"
  16. Android: layout_weight = "1"
  17. Android: foregroundgravity = "fill_horizontal | top"
  18. Android: foreground = "? Android: ATTR/windowcontentoverlay "/>
  19. </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">
  1. <Style name = "customwindowtitlebackground">
  2. <Item name = "Android: background"> #00cc00 </item>
  3. </Style>
  4. <Style name = "test" parent = "Android: Theme">
  5. <Item name = "Android: windowtitlesize"> 50dp </item>
  6. <Item name = "Android: windowtitlebackgroundstyle"> @ style/customwindowtitlebackground </item>
  7. </Style>
  8. </Resources>

Add to androidmanifest

<Activity Android: Name = ". customtitilebaractivity"
  1. Android: Label = "@ string/app_name" Android: theme = "@ style/title_bar">
  2. <Intent-filter>
  3. <Action Android: Name = "android. Intent. Action. Main"/>
  4. <Category Android: Name = "android. Intent. Category. launcher"/>
  5. </Intent-filter>
  6. </Activity>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.