1. Define the layout file and create a new title_bar_view.xml under layout
<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "48DP"Android:background= "@drawable/bg_top_bar" > <ImageViewAndroid:id= "@+id/bar_back"Android:layout_width= "48DP"Android:layout_height= "48DP"Android:scaletype= "Center"android:src= "@drawable/btn_back_selector" /> <TextViewAndroid:id= "@+id/bar_title"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:layout_alignparenttop= "true"Android:layout_centerhorizontal= "true"Android:layout_marginleft= "48DP"Android:layout_marginright= "48DP"android:gravity= "Center"Android:text= "Title"Android:textcolor= "#FFFFFFFF"android:textsize= "24SP"Android:textstyle= "normal" /> <TextViewAndroid:id= "@+id/bar_action"Android:layout_width= "48DP"Android:layout_height= "48DP"Android:layout_alignparentright= "true"android:layout_centervertical= "true"android:gravity= "Center"Android:text= "Action"Android:textcolor= "#FFFFFFFF"android:textsize= "18SP"Android:textstyle= "normal"android:visibility= "Gone" /></Relativelayout>
Related resources, create a new btn_back_selector.xml under drawable
<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android"> <Itemandroid:drawable= "@drawable/action_button_back_pressed"android:state_pressed= "true"/> <Itemandroid:drawable= "@drawable/action_button_back_normal"/></selector>
2. Custom view inherits from Relativelayout
Public classTitleBarextendsrelativelayout{PrivateImageView Ivback; PrivateTextView Tvtitle; PrivateTextView tvaction; PublicTitleBar (Context context) { This(Context,NULL); } PublicTitleBar (Context context, AttributeSet attrs) {Super(context, attrs); View View= View.inflate (Context, R.layout.title_bar_view, This); Ivback=(ImageView) View.findviewbyid (r.id.bar_back); Tvtitle=(TextView) View.findviewbyid (r.id.bar_title); Tvaction=(TextView) View.findviewbyid (r.id.bar_action); } Public voidSetbackvisibility (Booleanshow) {ivback.setvisibility (show?View.VISIBLE:View.INVISIBLE); } Public voidSettitle (String title) {Tvtitle.settext (title); } PublicImageView Getivback () {returnIvback; } Public voidSetivback (ImageView ivback) { This. Ivback =Ivback; } PublicTextView Gettvtitle () {returnTvtitle; } Public voidSettvtitle (TextView tvtitle) { This. Tvtitle =Tvtitle; } PublicTextView gettvaction () {returntvaction; } Public voidsettvaction (TextView tvaction) { This. tvaction =tvaction; }}
Common top headings and returned titlebar in the project