Customize top title bar in Android project
Here's a detailed introduction to the idea and implementation of customizing the top title bar in Android
First, figure out:
Ideas and implementation steps
1. Define the title bar layout
2. Custom Titleactivity control title bar button monitoring
3. Implement the following content toggle in the title bar in Titleactivity
- First define the title bar
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:id=" @+id/layout_titlebar "android:layout_width=" match_parent "android:layout_height=" 52DP "an droid:background= "#ed4255" > <textview android:id= "@+id/text_title" android:layout_width= "Match_par Ent "android:layout_height=" match_parent "android:ellipsize=" marquee "android:gravity=" Center_horizon Tal|center "android:singleline=" true "android:text=" title bar "android:textcolor=" #ffffffff "Android : textsize= "20DP"/> <button android:id= "@+id/button_backward" android:layout_width= "60DP" an droid:layout_height= "Match_parent" android:background= "@drawable/title_button_selector" Android:drawableleft = "@drawable/back_arrow" android:drawablepadding= "6DP" android:ellipsize= "End" android:gravity= "center "Android:onclick=" OnClick"Android:paddingleft=" 5DP "android:singleline=" true "android:text=" returns "Android:textcolor=" #ff FFFFFF "android:textsize=" 18DP "android:visibility=" invisible "/> <button android:id=" @+id/b Utton_forward "android:layout_width=" 60DP "android:layout_height=" Match_parent "ANDROID:LAYOUT_ALIGNP Arentright= "true" android:background= "@drawable/title_button_selector" android:drawablepadding= "6DP" Android:ellipsize= "End" android:gravity= "center" android:onclick= "OnClick" android:paddingleft= "5DP" Android:singleline= "true" android:text= "commit" android:textcolor= "#ffffffff" android:textsize= "18 DP "android:visibility=" invisible "/></relativelayout>
- Defines the layout that controls the contents of the title bar button and the title bar
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "Vertical" > <!--Title-- <include layout= "@layout/layout_titlebar"/> < Framelayout android:id= "@+id/layout_content" android:layout_width= "match_parent" android:layout_ height= "Match_parent" android:background= "#fff" > </FrameLayout></LinearLayout>
Note: The title bar is introduced here using the <include> tag, and there is an empty framelayout layout defined below.
- Define titleactivity control buttons and layouts
Package Org.gaochun.widget;import Org.gaochun.ui.r;import Android.app.activity;import android.os.Bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup.layoutparams;import Android.widget.button;import Android.widget.framelayout;import Android.widget.textview;import android.widget.toast;/** * @author Gao_chun * Custom title bar */public class Titleactivity extends Activity implements Onclickliste ner{//private relativelayout Mlayouttitlebar; Private TextView Mtitletextview; Private Button Mbackwardbbutton; Private Button Mforwardbutton; Private Framelayout mcontentlayout; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setupviews (); Load the Activity_title layout and get the caption and both sides of the button} private void Setupviews () {Super.setcontentview (r.layout.activity_title ); Mtitletextview = (TextView) Findviewbyid (r.id.text_title); Mcontentlayout = (framelayout) findvIewbyid (r.id.layout_content); Mbackwardbbutton = (Button) Findviewbyid (R.id.button_backward); Mforwardbutton = (Button) Findviewbyid (R.id.button_forward); }/** * Show return button * @param backwardresid text * @param show true displays */protected void Showbackwardvie W (int backwardresid, Boolean show) {if (Mbackwardbbutton! = null) {if (show) {Mbackwar Dbbutton.settext (BACKWARDRESID); Mbackwardbbutton.setvisibility (view.visible); } else {mbackwardbbutton.setvisibility (view.invisible); }}//Else ignored}/** * Provides whether the submit button is displayed * @param forwardresid text * @param show true to display */ protected void Showforwardview (int forwardresid, Boolean show) {if (Mforwardbutton! = null) {if (show ) {mforwardbutton.setvisibility (view.visible); Mforwardbutton.settext (FORWARDRESID); } else {mForwardbutton.setvisibility (view.invisible); }}//Else ignored}/** * Back button click to trigger * @param backwardview */protected void Onbackward (View Backwardview) {Toast.maketext (this, "Click Back, where you can call finish ()", Toast.length_long). Show (); Finish (); }/** * Submit button click Trigger * @param forwardview */protected void Onforward (View forwardview) {Toast.make Text (This, "click Submit", Toast.length_long). Show (); }//Set caption content @Override public void settitle (int titleid) {mtitletextview.settext (TitleID); }//Set caption content @Override public void Settitle (charsequence title) {Mtitletextview.settext (title); }//Set caption text color @Override public void settitlecolor (int textcolor) {mtitletextview.settextcolor (textcolor); }//Remove Framelayout and call the parent class Removeallviews () method @Override public void Setcontentview (int layoutresid) {Mconte Ntlayout.removeallviews (); View.inflate (This, Layoutresid, MCOntentlayout); Oncontentchanged (); } @Override public void Setcontentview (view view) {mcontentlayout.removeallviews (); Mcontentlayout.addview (view); Oncontentchanged (); }/* (non-javadoc) * @see Android.app.activity#setcontentview (Android.view.View, Android.view.ViewGroup.LayoutParam s) */@Override public void Setcontentview (view view, Layoutparams params) {Mcontentlayout.removeallview S (); Mcontentlayout.addview (view, params); Oncontentchanged (); }/* (non-javadoc) * @see Android.view.view.onclicklistener#onclick (Android.view.View) * button Click to invoke the method */@O Verride public void OnClick (View v) {switch (V.getid ()) {case R.id.button_backward: Onbackward (v); Break Case R.id.button_forward:onforward (v); Break Default:break; } }}
- Mainactivity is called directly extends Titleactivity uses the method previously defined in Titleactivity
source Download:http://download.csdn.net/download/gao_chun/8629827
Customize top title bar in Android project