Android Custom title bar

Source: Internet
Author: User

Lin Bingwen Evankaka original works. Reprint please specify the source Http://blog.csdn.net/evankaka

This article is to say that you define a title bar, can add events. Then you can move the value to a different phone, basically do not change anything, the call is very simple

under the Layout folder, create a new XML. The name is Layout_title_bar.xml and then look at the layout:

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" 45.0dip "android:background=" @drawable/bg_titl E_bar "android:gravity=" Top "> <imageview android:id=" @+id/title_bar_menu_btn "Android:layout_wi Dth= "Wrap_content" android:layout_height= "Wrap_content" android:layout_centervertical= "true" Android: layout_marginleft= "3.0dip" android:layout_marginright= "3.0dip" android:layout_margintop= "3.0dip" Android oid:gravity= "center" android:src= "@drawable/ic_top_bar_category"/> <imageview android:layout_width        = "Wrap_content" android:layout_height= "fill_parent" android:layout_torightof= "@id/title_bar_menu_btn" android:background= "@drawable/ic_top_divider"/> <textview android:id= "@+id/title_bar_name" Androi    D:layout_width= "Wrap_content"    android:layout_height= "Wrap_content" android:layout_centerinparent= "true" android:ellipsize= "End" android:gravity= "center" android:paddingleft= "75.0dip" android:paddingright= "75.0dip" Android:single Line= "true" android:text= "Java Learning Treasure" android:textcolor= "#ffffff" android:textsize= "22SP"/></rel Ativelayout>


Look at the effect:

The next thing to use, where to use it: I'm here in the Activity_main.xml file:

Add a sentence: <include layout= "@layout/layout_title_bar"/> This is OK,

Then we're going to add an event to the button on the title bar, which is much simpler:

In Mainactivity.java (corresponding to Activity_main.xml), the OnCreate function is added: The event can be changed by itself, I am here to let it control the function of sliding left and right.

ImageView menuimg = (ImageView) Findviewbyid (R.ID.TITLE_BAR_MENU_BTN); Menuimg.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View arg0) {if (!menuisshow) ShowMenu (); else {hidemenu ();}});


This will do the following:

Let's take a look at the effect

This is the effect, very simple, want to use the above layout directly to copy the past is OK!

Android Custom title bar

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.