Android Custom title bar (titles bar) instance

Source: Internet
Author: User

The first step is to implement a custom title bar that needs to be written in the OnCreate method

Requestwindowfeature (Window.feature_custom_title);

Setcontentview (R.layout.main);

GetWindow (). Setfeatureint (Window.feature_custom_title, R.layout.title_bar);

Attention:
Requestwindowfeature to Setcontentview before the
GetWindow () Setfeatureinit preferably after Setcontentview.


The second step is to write your own layout files, to achieve the custom title bar.

However, we will encounter some problems, that is, the height of the title bar can not be customized ~ The following is the solution ~

Here's an example to see how to do this.

1, under the layout to create a titlebtn.xml file, the contents are as follows:

The code is as follows Copy Code

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= "Fill_parent"

android:orientation= "Horizontal" >


<<>imagebutton

Android:id= "@+id/imagebutton1"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

Android:layout_alignparentleft= "true"

Android:layout_centervertical= "true"

Android:background= "#00000000"

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:layout_alignparentright= "true"

Android:layout_centerinparent= "true"

Android:background= "#00000000"

android:src= "@drawable/next"/>
<RelativeLayout>


modifying Style.xml files

Add the following code

The code is as follows Copy Code

Name= "Customwindowtitlebackground" >

Name= "Android:background" > #00cc00

Name= "Test" parent= "Android:theme" >

Name= "Android:windowtitlesize" >50DP

Name= "Android:windowtitlebackgroundstyle" > @style/customwindowtitlebackground

Add to Androidmanifest

  code is as follows copy code


Android : Name= ". Customtitilebaractivity "
android:label=" @string/app_name "android:theme=" @style/test "
Android:name=" Android.intent.action.MAIN "/>
 android:name=" Android.intent.category.LAUNCHER "/>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.