Android Development How to customize TitleBar in 4.0 and above systems

Source: Internet
Author: User

This article will explain how to implement the custom TitleBar in the system version 4.0 and above through an example, which is just a method that I find myself;

XML layout file

Activity_main.xml

  1. <relativelayout xmlns:android="Http://schemas.android.com/apk/res/android"
  2. xmlns:tools="Http://schemas.android.com/tools"
  3. Android:layout_width="Match_parent"
  4. android:layout_height="Match_parent"
  5. android:paddingbottom="@dimen/activity_vertical_margin"
  6. android:paddingleft="@dimen/activity_horizontal_margin"
  7. android:paddingright="@dimen/activity_horizontal_margin"
  8. android:paddingtop="@dimen/activity_vertical_margin"
  9. Tools:context=". Mainactivity ">
  10. <textview
  11. Android:layout_width="Wrap_content"
  12. android:layout_height="Wrap_content"
  13. android:text="@string/hello_world"/>
  14. </RelativeLayout>

Custom titlebar Layout File Titlebar.xml

  1. <?xml version="1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"
  3. Android:layout_width="Match_parent"
  4. android:layout_height="48DP"
  5. android:orientation="Horizontal" >
  6. <imageview
  7. Android:layout_width="Wrap_content"
  8. android:layout_height="Wrap_content"
  9. android:layout_gravity="center"
  10. android:layout_weight="1.5"
  11. android:src="@drawable/ic_action_search"/>
  12. <textview
  13. Android:layout_width="Wrap_content"
  14. android:layout_height="48DP"
  15. android:layout_weight="1.5"
  16. android:paddingtop="1DP"
  17. android:text="@string/app_name"
  18. android:textsize="14sp"/>
  19. <edittext
  20. android:id="@+id/searchparameter"
  21. Android:layout_width="Wrap_content"
  22. android:layout_height="48DP"
  23. android:layout_margin="1DP"
  24. android:layout_weight="5"
  25. android:text="Abcdefghij"
  26. android:textsize="14sp"/>
  27. <button
  28. android:id="@+id/button"
  29. Android:layout_width="Wrap_content"
  30. android:layout_height="48DP"
  31. android:layout_margin="1DP"
  32. android:layout_weight="2"
  33. android:text="OK"
  34. android:textsize="14sp"/>
  35. </LinearLayout>

Modify Style.xml for a layout file

Here the Style.xml in values-11 or value-14, the side will be error: You cannot combine custom titles with other title features

  1. <?xml version="1.0" encoding="Utf-8"?>
  2. <resources xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <style name="Customizedwindowtitlebackground" >
  4. <item name="Android:background" > #047BF0 </item>
  5. </style>
  6. <style name="Titlebarstyle" parent="Android:theme" >
  7. <item name="android:windowtitlesize" >48dp</item>
  8. <item name="Android:windowtitlebackgroundstyle" >@style/customizedwindowtitlebackground</item >
  9. <!--all customizations that is not specific to a particular api-level can go. -
  10. </style>
  11. </resources>

Add Theme styles to Androidmanifest.xml

Android:theme= "@style/titlebarstyle"

  1. <?xml version="1.0" encoding="Utf-8"?>
  2. <manifest xmlns:android="Http://schemas.android.com/apk/res/android"
  3. package="Com.example.titlebardemo"
  4. android:versioncode="1"
  5. Android:versionname="1.0" >
  6. <uses-sdk
  7. android:minsdkversion="8"
  8. android:targetsdkversion="/> "
  9. <application
  10. android:allowbackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. Android:label="@string/app_name" >
  13. <activity
  14. Android:name="Com.example.titlebardemo.MainActivity"
  15. Android:label="@string/app_name"
  16. Android:theme="@style/titlebarstyle" >
  17. <intent-filter>
  18. <action android:name="Android.intent.action.MAIN"/>
  19. <category android:name="Android.intent.category.LAUNCHER"/>
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </manifest>

Add implementation code in Mainactivity.java

    1. protected void OnCreate (Bundle savedinstancestate) {
    2. Super.oncreate (savedinstancestate);
    3. Requestwindowfeature (Window.feature_custom_title);
    4. Setcontentview (R.layout.activity_main);
    5. GetWindow (). Setfeatureint (Window.feature_custom_title,
    6. R.layout.titlebar);
    7. }

Code in Requestwindowfeature (Window.featurecustomtitle), and GetWindow (). Setfeatureint (Window.featurecustomtitle, R.layout.titlebar); The position is fixed.

Readers are expected to use this example to have a deep understanding and mastery of how to implement custom TitleBar in the 4.0 and above system versions.

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.