Android custom title bar and android title bar

Source: Internet
Author: User

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!

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.