Android custom title bar and android title bar
This article describes how to define a title bar to add events. Then you can move the value to different mobile phones. Basically, there is no need to change the value, and the call is very simple.
In the layout folder, create an XML file. The name is layout_title_bar.xml and you can see 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_title_bar" android: gravity = "top"> <ImageView android: id = "@ + id/title_bar_menu_btn" android: layout_width = "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: gravity = "center" android: src = "@ drawable/ic_top_bar_category"/> <ImageView android: layout_width = "wrap_content" android: layout_height = "fill_parent" android: layout_toRightOf = "@ id/category" android: background = "@ drawable/ic_top_divider"/> <TextView android: id = "@ + id/title_bar_name" android: 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: singleLine = "true" android: text = "" android: textColor = "# ffffff" android: textSize = "22sp"/> </RelativeLayout>
See the following results:
Next, I want to use it. Here is the activity_main.xml file:
Add the following sentence: <include layout = "@ layout/layout_title_bar"/>,
Then we need to add an event to the button on the title bar, which is simpler:
In MainActivity. java (corresponding to activity_main.xml), add the event to the onCreate function. You can modify the event by yourself. Here, let it control the left and right sliding function.
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();}}});
In this way, you can:
Let's take a look at the effect.
This is the effect. It's very easy. If you want to copy the layout above, it will be OK!