Android-custom title bar (add buttons and text center in the title bar)

Source: Internet
Author: User

Many Android programs now display buttons and titles on the title bar, such:

 

The following example shows how to implement it.

1. Create a titlebtn. xml file under layout with the following content:

[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "horizontal"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">

<ImageButton
Android: id = "@ + id/imageButton1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: background = "#00000000"
Android: layout_centerVertical = "true"
Android: layout_alignParentLeft = "true"
Android: src = "@ drawable/prv"/>
 
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_centerInParent = "true"
Android: text = "title bar"/>
 
<ImageButton
Android: id = "@ + id/imageButton1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: background = "#00000000"
Android: layout_centerInParent = "true"
Android: layout_alignParentRight = "true"
Android: src = "@ drawable/next"/>

</RelativeLayout>
Note the following when creating this xml:

A) Layout with RelativeLayout

B) In particular, layout_centerInParent must be specified for the properties of the right button.


2. Add the following code to onCreate in the Activity:
[Java]
RequestWindowFeature (Window. FEATURE_CUSTOM_TITLE );
SetContentView (R. layout. main );
GetWindow (). setFeatureInt (Window. FEATURE_CUSTOM_TITLE, R. layout. titlebtn );

The above two steps can be used to achieve the above effect.

From rough roads

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.