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