Custom top title bar and Android title bar in the android Project

Source: Internet
Author: User

Custom top title bar and Android title bar in the 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

<? 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" android: background = "# ed4255"> <TextView android: id = "@ + id/text_title" android: layout_width = "match_parent" android: layout_height = "match_parent" android: ellipsize = "marquee" android: gravity = "center_horizontal | center" android: singleLine = "true" android: text = "title bar" android: textColor = "# ffffffff" android: textSize = "20dp"/> <Button android: id = "@ + id/button_backward" android: layout_width = "60dp" android: 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 = "return" android: textColor = "# ffffffff" android: textSize = "18dp" android: visibility = "invisible"/> <Button android: id = "@ + id/button_forward" android: layout_width = "60dp" android: layout_height = "match_parent" android: plugin = "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 = "Submit" android: textColor = "# ffffffff" android: textSize = "18dp" android: visibility = "invisible"/> </RelativeLayout>


  • Define the layout of the control title bar button and the content below 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:orientation="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:Here, the <include> label is used to introduce 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 to return, 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 to 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.





Download source code:Http://download.csdn.net/download/gao_chun/8629827



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.