One after another activity, write AH write, tune ah tune, later, finally found that the activity of the title bar ugly, good monotonous ah. We have to do something personal to attract the attention of the users.
Custom title bar method, a search a lot of online, I also slightly mention, oncreate add the following code to line:
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);
This layout named title is like this, very simple, is a textview, then there is a background color:
XML code
<!--<br/><br/>code highlighting produced by Actipro Codehighlighter (freeware) <br/>http://www. codehighlighter.com/<br/><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= "#66cccccc"
>
<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Hello"
/>
</LinearLayout>
Well, run to see the effect. See, found the problem no, the title bar background color is not filled with it, this is really a cup of yo. padding, margin and so on all use also no use, how to do it.
Look at the source!
Window initialization, loading the title of the place, we do not know where, but we can use layout as a starting point. Open the Layout folder inside the source code and find the XML file associated with the title bar. There are screen_title.xml and Screen_custom_title.xml, and that's the target we're looking for.
Since it's a custom title, we'll look at Screen_custom_title.xml, which has a title_container and a content, which is combined into a title bar, and the view given by our custom title is a child view of the content. , can not affect that title_container and content, so, let you how to do, it should stay white or stay white, you did not recruit.
Look Title_container has a style like this: style= "? Android:attr/windowtitlebackgroundstyle"
The foreground of content is such android:foreground= "? Android:attr/windowcontentoverlay"
Well, from here we can begin to change.
Go to values below the Themes.xml find Windowtitlebackgroundstyle this one, this should be under the comment.
XML code
<!--<br/><br/>code highlighting produced by Actipro Codehighlighter (freeware) <br/>http://www. codehighlighter.com/<br/><br/>--><item name= "Windowtitlebackgroundstyle" > @android: style/ Windowtitlebackground</item>
Then go to Styles.xml and find the Windowtitlebackground,
XML code
<!--<br/><br/>code highlighting produced by Actipro Codehighlighter (freeware) <br/>http://www. codehighlighter.com/<br/><br/>--><style name= "Windowtitlebackground" >
<item name= "Android:background" > @android:d rawable/title_bar</item>
</style>
The discovery is a drawable,xml, which defines the background image. OK, we know, this is the background color that defines titlebar.
Then, go to values below the Themes.xml find Windowcontentoverlay, also belong to window attributes.
XML code
<!--<br/><br/>code highlighting produced by Actipro Codehighlighter (freeware) <br/>http://www. codehighlighter.com/<br/><br/>--><item name= "Windowcontentoverlay" > @android:d rawable/title _bar_shadow</item>
Discovery is also a drawable,ok, we also know, this is the definition of contentoverlay background.
In fact, through the study I found that can not fill the reason is the background of the Title_container, we cover a bit on the line.
First, write a themes file.
XML code
<!--<br/><br/>code highlighting produced by Actipro Codehighlighter (freeware) <br/>http://www. Codehighlighter.com/<br/><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/><br/>code highlighting produced by Actipro Codehighlighter (freeware) <br/>http://www. Codehighlighter.com/<br/><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 to inherit WindowTitle.
Finally, customize the activity in Manifext to declare the topic.
XML code
<!--<br/><br/>code highlighting produced by Actipro Codehighlighter (freeware) <br/>http://www. codehighlighter.com/<br/><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>
OK, let's take a look at the effect:
So cool, isn ' t it?
Of course, you can also change to a different color or a more dazzling picture to do the background.
Android Development-Custom title bar