"Android" implements a title bar of its own

Source: Internet
Author: User

Full project Download

Background: The project uses the title bar, just a simple include a title bar view, assignment, control element display, click the event to do their own, not elegant!

Requirements:

1: Minimal intrusion to existing code

2: Simple enough to use

OK, around this demand, we made a standard title bar. There is text in the middle, the left and right sides can be text or pictures.

The display of the title bar and the left text is called as follows:

< Net.xuele.xuelets.ui.ActionBarLeftRightButton         Android:layout_width = "Match_parent"         android:layout_height= "@dimen/title_bar_height"        app:titleleftimage= "@ Mipmap/arrow_back "        app:titletext=" Extract Treasure "/>

Background events: Control elements hide, set click events?

The answer is not a word. 66666666

How did it come true?

We have set a style, the text priority is higher than the picture. Which property you set, which property corresponds to the control, is displayed.

This control determines whether the vector context implements the Click event, and if so, automatically adds the OnClick event to the displayed control.

 <declare-styleablename= "Actionbarleftrightbutton">        <attrname= "Titlelefttext"format= "string"/>        <attrname= "Titleleftimage"format= "Reference"/>        <attrname= "TitleText"format= "string"/>        <attrname= "Titlerighttext"format= "string"/>        <attrname= "Titlerightimage"format= "Reference"/>    </declare-styleable>

So all you have to do in the background is to write the corresponding ID in the onclick event.

After the actual use, the experience is much better than before, elegant ~

The core code is as follows:

 Packagezhexian.app.myapplication;ImportAndroid.content.Context;ImportAndroid.content.res.TypedArray;Importandroid.graphics.drawable.Drawable;Importandroid.text.TextUtils;ImportAndroid.util.AttributeSet;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.widget.ImageButton;Importandroid.widget.RelativeLayout;ImportAndroid.widget.TextView;/*** title bar, left and right buttons, pictures or text can be * priority text, followed by the picture, both do not display * Reference Properties R.styleable.actionbarleftrightbutton * Created by Junjie on 2015/12/ 8.*/ Public classActionbarleftrightbuttonextendsRelativelayout { PublicActionbarleftrightbutton (Context context) { This(Context,NULL, 0); }     PublicActionbarleftrightbutton (Context context, AttributeSet attrs) { This(context, Attrs, 0); }     PublicActionbarleftrightbutton (context context, AttributeSet attrs,intdefstyleattr) {        Super(context, attrs, defstyleattr);    Initview (context, attrs); }    /*** initialization, requires context implementation of Onclicklistener * *@paramContext *@paramAttrs*/    voidInitview (Context context, AttributeSet attrs) {View View= Layoutinflater.from (context). Inflate (R.LAYOUT.VIEW_ACTIONBAR_LEFT_RIGHT_BTN, This,true);        Setbackgroundresource (R.color.orange); TypedArray Attrarray=context.obtainstyledattributes (Attrs, R.styleable.actionbarleftrightbutton); BooleanIsholdercanclick = ContextinstanceofOnclicklistener; Onclicklistener Onclicklistener= Isholdercanclick? (Onclicklistener) Context:NULL; Bindnavigateaction (View,true, Attrarray, Onclicklistener); Bindnavigateaction (View,false, Attrarray, Onclicklistener); String TitleText=attrarray.getstring (R.styleable.actionbarleftrightbutton_titletext); if(!Textutils.isempty (TitleText)) Bindtextview ((TextView) View.findviewbyid (R.id.title_text), TitleText, OnClic        Klistener);    Attrarray.recycle (); }    /*** Bind the left or right button, text *@paramView *@paramIsleft *@paramAttrarray *@paramOnclicklistener*/    voidBindnavigateaction (View view,BooleanIsleft, TypedArray Attrarray, Onclicklistener Onclicklistener) {String Lefttext= Attrarray.getstring (isleft?)r.styleable.actionbarleftrightbutton_titlelefttext:r.styleable.actionbarleftrightbutton_titlerighttext); if(!Textutils.isempty (Lefttext))        {Bindtextview (view, Lefttext, Isleft, Onclicklistener); } Else{drawable Leftimage= Attrarray.getdrawable (isleft?)r.styleable.actionbarleftrightbutton_titleleftimage:r.styleable.actionbarleftrightbutton_titlerightimage); if(Leftimage! =NULL) Bindimageview (view, Leftimage, Isleft, Onclicklistener); }    }    voidBindtextview (view view, String text,BooleanIsleft, Onclicklistener Onclicklistener) {Bindtextview (TextView) View.findviewbyid (Isleft?r.id.title_left_text:r.id.title_right_text), text, Onclicklistener); }    /*** Binding Text * *@paramTextView *@paramtext *@paramOnclicklistener*/    voidBindtextview (TextView TextView, String text, Onclicklistener onclicklistener) {textview.setvisibility (VISIBLE        );        Textview.settext (text); if(Onclicklistener! =NULL) Textview.setonclicklistener (Onclicklistener); }    /*** Bind picture * *@paramView *@paramdrawable *@paramIsleft *@paramOnclicklistener*/    voidBindimageview (view view, Drawable drawable,BooleanIsleft, Onclicklistener Onclicklistener) {ImageButton button= (ImageButton) View.findviewbyid (isleft?)R.id.title_left_button:r.id.title_right_button);        Button.setvisibility (VISIBLE);        Button.setimagedrawable (drawable); if(Onclicklistener! =NULL) Button.setonclicklistener (Onclicklistener); }}

Android implements a title bar of its own

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.