Custom top title bar in Android Project
Custom top title bar in Android Project
The following describes in detail the ideas and implementation methods of customizing the top title bar in android.
Diagram:
Ideas and implementation steps
1. Define the title bar Layout
2. Custom TitleActivity control title bar button monitoring
3. Switch the content below the title bar in TitleActivity
First define the title bar
Define the layout of the control title bar button and the content below the title bar
<framelayout android:background="#fff" android:id="@+id/layout_content" android:layout_height="match_parent" android:layout_width="match_parent"> </framelayout>
Note:Used here The label is introduced into the title bar, and an empty FrameLayout layout is defined below.
Define TitleActivity control buttons and Layout
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 exten Ds Activity implements OnClickListener {// private RelativeLayout listener; private TextView mTitleTextView; private Button mBackwardbButton; private Button listener; private protected mContentLayout; @ Override protected void onCreate (Bundle listener) {super. onCreate (savedInstanceState); setupViews (); // load the activity_title layout and obtain the title and buttons on both sides.} private void setupViews () {super. s EtContentView (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);}/*** whether to display the return button * @ param backwardResid text * @ param show true indicates display */protected void showBackwardView (int backwa RdResid, boolean show) {if (mBackwardbButton! = Null) {if (show) {mBackwardbButton. setText (backwardResid); mBackwardbButton. setVisibility (View. VISIBLE);} else {mBackwardbButton. setVisibility (View. INVISIBLE) ;}}// else ignored}/*** provides whether to display the submit button * @ 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}/*** click the return button to trigger * @ param backwardView */protected void onBackward (View backwardView) {Toast. makeText (this, click back, you can call finish (), Toast. LENGTH_LONG ). show (); // finish ();}/*** click the submit button to trigger * @ param forwardView */protected void onForward (View forwardView) {Toast. makeText (this, click Submit, Toast. LENGTH_LONG ). show () ;}// set the title content @ Override public void setTitle (int titleId) {mTitleTextView. setText (titleId);} // set the title content @ Override public void setTitle (CharSequence title) {mTitleTextView. setText (title) ;}// set the title text color @ Override public void setTitleColor (int textColor) {mTitleTextView. setTextColor (textColor);} // retrieves FrameLayout and calls the removeAllViews () method of the parent class @ Override public void setContentView (int layoutResID) {mContentLayout. 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. layoutParams) */@ Override public void setContentView (View view, LayoutParams params) {mContentLayout. removeAllViews (); mContentLayout. addView (view, params); onContentChanged ();}/* (non-Javadoc) * @ see android. view. view. onClickListener # onClick (android. view. view) * click the call Method */@ Override 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 ;}}}
When called in MainActivity, extends TitleActivity uses the method defined in TitleActivity.