Android modified TitleBar title bar detailed

Source: Internet
Author: User

Recently the project has an activity theme set to the dialog popup box style, found that the title bar height and font are too small, so looked up the relevant information and source code, summed up the method of modification. The height is modified by modifying the android:windowtitlesize, font size by style.

The default activity title bar for Android programs can only display a piece of text, and cannot change its layout, color, title bar height, and so on. If you want to add an icon, button, input box, progress bar, change title bar color, and so on in the title bar, you can only use the custom title bar. The custom title bar can be implemented by adding the following code to the OnCreate function, noting that the order of the code must follow the following style, otherwise it will not be valid. Note that it is now recommended to use the latest toolbar controls to solve the title bar customization problem, and the other methods are obsolete.

public static void Settitle (activity activity, int resId) {        activity.requestwindowfeature (window.feature_custom_ TITLE);        Activity.setcontentview (RESID); Activity Layout        Activity.getwindow (). Setfeatureint (Window.feature_custom_title, r.layout.custom_title);// Layout of the title bar    }

Examples of toolbar:

<?xml version= "1.0" encoding= "Utf-8"? ><android.support.v7.widget.toolbar xmlns:android= "/http Schemas.android.com/apk/res/android "    xmlns:app=" Http://schemas.android.com/apk/res-auto "    android:id=" @ +id/toolbar "    android:layout_width=" match_parent "    android:layout_height="? Attr/actionbarsize    " android:background= "@drawable/common_toolbar_background"    android:elevation= "5DP"    app:theme= "@style/ ThemeOverlay.AppCompat.Dark.ActionBar ">    <textview        android:id=" @+id/toolbar_title "        style=" @ Style/toolbartextappearance "        android:layout_width=" wrap_content "        android:layout_height=" wrap_content "        android:layout_gravity=" center "/></android.support.v7.widget.toolbar>



Although the above can be added to the title bar some controls, but still can not change the title bar height, background color, to achieve this goal, you can only use Theme (theme). So add a style to project first. Change the background color to modify the value of the Android:windowtitlebackgroundstyle, change the height of the title bar to modify the Android:windowtitlesize value. Here is an example:

<style name= "Dialogtextappearance" >        <item name= "android:textsize" >30sp</item>        <item Name= "android:layout_gravity" >center</item>        <item name= "android:padding" >10dp</item>    </style>    <style name= "Dialogtitlebar" parent= "Android:Theme.Holo.Light.Dialog" >        < Item Name= "Android:windowtitlesize" >60sp</item>        <item name= "Android:windowtitlestyle" > @style/ Dialogtextappearance</item>    </style>

Modify the Androidmanifest.xml file, find the activity to customize the title bar, add the Android:theme value


<!--Bluetooth device-            -<activity android:name= ". Gprinter. Bluetoothdevicelist "            android:label=" @string/bluetooth_device_list "            android:theme=" @style/ Dialogtitlebar "            android:configchanges=" Keyboardhidden|orientation|screensize "            >

The Android:theme value is the name value of a style defined in the Style.xml file above.

According to the above steps, the function of modifying title bar layout, height and background color is realized.

Copyright NOTICE: This article is the original blogger article, reproduced please retain the source Http://blog.csdn.net/offbye

Android modified TitleBar title bar detailed

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.