Android Custom Activity title bar (titlebar)

Source: Internet
Author: User

By default, the title bar (titlebar) of the activity is usually seen (in red box):

Handlecontacts is the title of the activity.
Sometimes, we want to change the monotony of this situation. For example, how do you do this by adding an icon to beautify the interface, adding an input box, or a button to the title bar? We might as well look at a practical example.

1. First create an Android project as follows

2. Drag the picture magnifier.png into the project's res/drawable-mdpi folder. The Magnifier.png picture looks like this:

3. Under the Res/layout folder of the project, create a layout titlebar.xml that will be used to customize the activity's title bar

Edit the Titlebar.xml so that it reads as follows:

<?xmlversion= "1.0" encoding= "Utf-8"?>
<linearlayout
Xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Horizontal"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content" >
<imageview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:src= "@drawable/magnifier"
android:gravity= "Bottom"
/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "38dip"
android:text= "@string/app_name"
Android:textcolor= "#FFFFFFFF"
Android:textsize= "14dip"
android:paddingtop= "1dip"
/>
<edittext
Android:id= "@+id/searchparameter"
Android:layout_width= "Wrap_content"
android:layout_height= "38dip"
android:text= "Abcdefghij"
Android:textsize= "14dip"
Android:layout_margin= "1dip"
/>
<button android:id= "@+id/button"
Android:layout_width= "Wrap_content"
android:layout_height= "38dip"
Android:text= "OK"
Android:textsize= "14dip"
/>
</LinearLayout>

In the linearlayout above, the following controls have been added:

A imageview that displays an icon

A textview that displays the name of the app

A edittext for receiving input

A button that is used to test

4. Modify the Customizetitlebar.java to make it as follows:

public class Customizetitlebar extends Activity
{
@Override
public void OnCreate (Bundle savedinstancestate)
{
Super.oncreate (savedinstancestate);
Requestwindowfeature (Window.feature_custom_title);
Setcontentview (R.layout.main);
GetWindow (). Setfeatureint (Window.feature_custom_title, R.layout.titlebar);
}
}

The two lines above are important, and must be in the code exactly as they are in the order shown above. That

Requestwindowfeature (Window. Feature_custom_title); must appear in Super.oncreate (savedinstancestate); Setcontentview (r.layout. Main); before. Its meaning is to tell the system, this procedure to define titlebar itself;

GetWindow (). Setfeatureint (Window. Feature_custom_title, R.layout. titlebar); Must appear after Setcontentview, which means telling the system that the custom layout is R.layout.titlebar (that is, the titlebar.xml we wrote earlier)

Here, let's run it and see what the results are:

We see that titlebar basically changes according to our meaning, but there is a flaw: TitleBar is too narrow, so how to change the height of titlebar?

5. To change the height of the titlebar, we have to create the Styles.xml (the directory is generally under/res/values/) and edit the styles.xml so that it reads as follows:

<?xmlversion= "1.0" encoding= "Utf-8"?>
<resources>
<style name= "Titlebarstyle" parent= "Android:theme" >
<item name= "Android:windowtitlesize" >38dip</item>
</style>
</resources>

Above <item name="android:windowtitlesize">39dip</item> This sentence, is used to set the height of titlebar.

6. Based on the above, we need to modify the properties of the corresponding activity in Androidmanifest.xml. As follows:

<?xmlversion= "1.0" encoding= "Utf-8"?>
<manifestxmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Com.pat.customizetitlebar"
Android:versioncode= "1"
Android:versionname= "1.0" >
<application android:icon= "@drawable/icon" android:label= "@string/app_name" >
<activity android:name= ". Customizetitlebar "
Android:label= "@string/app_name"
Android:theme= "@style/titlebarstyle" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
<USES-SDK android:minsdkversion= "8"/>
</manifest>

Note that the bold word is added, and the titlebar is added in the 5th step. Now let's look at the results of the operation:

We can see that the results are in full compliance with our requirements.

7. We can also change the background color of titlebar. For this we modify the previous styles.xml to make it as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<style name= "Customizedwindowtitlebackgroundcolor" >
<item name= "Android:background" > #047BF0 </item>
</style>
<style name= "Titlebarstyle" parent= "Android:theme" >
<item name= "Android:windowtitlesize" >38dip</item>
<item name= "Android:windowtitlebackgroundstyle" > @style/customizedwindowtitlebackgroundcolor</item>
</style>
</resources>

Note that the bolded characters are newly added.

No changes are required for other project documents. The results of the operation are as follows:

8. Finally, we use the OK button as an example to test the event response of the control on the titlebar. To do this, modify the Customizetitlebar.java to make it as follows:

1  Public classCustomizetitlebarextendsActivityImplementsOnclicklistener2 {3  Privatebutton button;4 @Override5      Public voidonCreate (Bundle savedinstancestate)6     {7        Super. OnCreate (savedinstancestate);8 requestwindowfeature (window.feature_custom_title);9 Setcontentview (r.layout.main);Ten GetWindow (). Setfeatureint (Window.feature_custom_title, r.layout.titlebar); Onebutton =(Button) Findviewbyid (R.id.button); AButton.setonclicklistener ( This); -  }  -   PublicVoidonclick (View v) the  { -  if(V.getid () = =R.id.button) -  { -Toast.maketext ( This, "OK button in TitleBar clicked ...", Toast.length_long). Show (); +  } -  } +}

The bold part is the newly added code. Rerun this project, and when the interface comes out, click on the OK button on the titlebar and it will appear:

This shows that the titlebar on its own added controls can respond well to related events.

Source: http://blog.csdn.net/pathuang68/article/details/6646792

Android Custom Activity title bar (titlebar)

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.