Android development-custom title bar and Android custom title bar

Source: Internet
Author: User

Android development-custom title bar and Android custom title bar

One activity goes on, writing, debugging, and debugging. Later, I realized that the title bar of the activity is ugly and monotonous. To attract users' attention, we need to make some personalized things.

The method for customizing the title bar is a lot of online searches. I will also mention it a bit. Just add the following code in oncreate:

Java code

RequestWindowFeature (Window. FEATURE_CUSTOM_TITLE );

SetContentView (view );

GetWindow (). setFeatureInt (Window. FEATURE_CUSTOM_TITLE, R. layout. title );

RequestWindowFeature (Window. FEATURE_CUSTOM_TITLE );

SetContentView (view );

GetWindow (). setFeatureInt (Window. FEATURE_CUSTOM_TITLE, R. layout. title );

The layout named title is like this. It is very simple. It is a textview with a background color:

Xml Code

<! -- <Br/> Code highlighting produced by Actipro CodeHighlighter (freeware) <br/> http://www.CodeHighlighter.com/<br/> --> <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: background = "#66 cccccc"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "hello"
/>
</LinearLayout>

 

Good. Run it to see the effect. You can see it. If you find the problem, the background color of the title bar is not filled with it. This is really a cup. What should I do if padding and margin are all used.

Check the source code!

When the window is initialized and the title is loaded, we don't know where it is, but we can use layout as the starting point. Open the layout folder in the source code and find the xml file related to the title bar. There are screen_title.xml and screen_custom_title.xml, which are our targets.

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"

Okay. Now we can start to change it.

Find windowTitleBackgroundStyle in themes. xml under values, which should be under the comment.

Xml Code

<! -- <Br/> Code highlighting produced by Actipro CodeHighlighter (freeware) <br/> http://www.CodeHighlighter.com/<br/> --> <item name = "windowTitleBackgroundStyle"> @ android: style/WindowTitleBackground </item>

 

Go to styles. xml and find the WindowTitleBackground item,

Xml Code

<! -- <Br/> Code highlighting produced by Actipro CodeHighlighter (freeware) <br/> http://www.CodeHighlighter.com/<br/> --> <style name = "WindowTitleBackground">
<Item name = "android: background"> @ android: drawable/title_bar </item>
</Style>

 

It is found to be a drawable, xml, which defines the background image. OK. Now we know that this is the background color that defines titlebar.

Then, find windowContentOverlay in themes. xml under values, which is also a window attributes.

Xml Code

<! -- <Br/> Code highlighting produced by Actipro CodeHighlighter (freeware) <br/> http://www.CodeHighlighter.com/<br/> --> <item name = "windowContentOverlay"> @ android: drawable/title_bar_shadow </item>

 

 

It is also a drawable and OK. We also know that this is the background that defines contentoverlay.

In fact, through research, I found that the reason why title_container cannot be filled is the background. We can just overwrite it.

First, write a themes file.

Xml Code

<! -- <Br/> Code highlighting produced by Actipro CodeHighlighter (freeware) <br/> http://www.CodeHighlighter.com/<br/> --> <resources>
<Style name = "XTheme" parent = "android: Theme">

<! -- Window attributes -->
<Item name = "android: windowTitleStyle"> @ style/XWindowTitle </item>
<Item name = "android: windowTitleBackgroundStyle"> @ style/StatusBarBackground </item>
<Item name = "android: windowContentOverlay"> @ null </item>
</Style>
</Resources>

 

Then write the styles File

Xml Code

<! -- <Br/> Code highlighting produced by Actipro CodeHighlighter (freeware) <br/> http://www.CodeHighlighter.com/<br/> --> <resources>
<Style name = "StatusBarBackground">
<Item name = "android: background"> @ drawable/shape </item>
</Style>

<Style name = "XWindowTitle" parent = "android: WindowTitle">
<Item name = "android: shadowColor"> # BB000000 </item>
<Item name = "android: shadowRadius"> 0 </item>
</Style>
</Resources>

 

Note that this XWindowTitle must inherit WindowTitle.

Finally, declare the topic for the custom activity in manifext.

Xml Code

<! -- <Br/> Code highlighting produced by Actipro CodeHighlighter (freeware) <br/> http://www.CodeHighlighter.com/<br/> --> <activity android: name = ". entry"
Android: label = "@ string/app_name"
Android: theme = "@ style/XTheme">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>

 

Okay. Let's take a look at the effect:

  

So cool, isn' t it?

Of course, you can also change to another color or a more dazzling image as the background.

Related Article

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.