Custom composite controls for Android Custom View--title bar

Source: Internet
Author: User
Tags getcolor

1 Implementation Effect

2 example code explained 2.1 Attrs.xml
<?xml version= "1.0" encoding= "Utf-8"?><resources>    <declare-styleable name="Topbar">        <attr name="title" format="string" />        <attr name="titletextsize" format="Dimension" / >        <attr name="Titletextcolor" format="Color" />        <attr name="Lefttextcolor" format="Color" />        <attr name="Leftbackground" format="Reference|color" />        <attr name="lefttext" format="string" />        <attr name="Righttextcolor" format="Color" />        <attr name="Rightbackground" format="Reference|color" />        <attr name="Righttext" format="string" />    </declare-styleable></Resources>
2.2 Topbar.xml
<com.xys.mytopbar.topbar xmlns:android="Http://schemas.android.com/apk/res/android"xmlnsCustom="Http://schemas.android.com/apk/res-auto"Android:id="@+id/topbar"Android:layout_width="Match_parent"android:layout_height="40DP"    Custom: leftbackground="@drawable/blue_button"    Custom: lefttext="Back"    Custom: lefttextcolor="#FFFFFF"    Custom: rightbackground="@drawable/blue_button"    Custom: righttext="More"    Custom: righttextcolor="#FFFFFF"    Custom: title="Custom title"    Custom: titletextcolor="#123412"    Custom: titletextsize="15SP"></com.xys.mytopbar.Topbar>
2.3 Topbar.java
 PackageCom.imooc.systemwidget; Public  class topbar extends relativelayout {    //contains elements on Topbar: Left button, right button, title    PrivateButton Mleftbutton, Mrightbutton;PrivateTextView Mtitleview;//Layout properties to control the position of component elements in ViewGroup    PrivateLayoutparams Mleftparams, Mtitlepparams, Mrightparams;The property value of the//left button, which is the attribute we defined in the Atts.xml file    Private intMlefttextcolor;PrivateDrawable Mleftbackground;PrivateString Mlefttext;//Right Button property value    Private intMrighttextcolor;PrivateDrawable Mrightbackground;PrivateString Mrighttext;//Title Property value    Private floatMtitletextsize;Private intMtitletextcolor;PrivateString Mtitle; Public Topbar(Context context) {Super(context); } Public Topbar(context context, AttributeSet attrs,intDefstyle) {Super(Context, attrs, Defstyle); } Public Topbar(context context, AttributeSet attrs) {Super(context, attrs);//Set the background of the TopbarSetBackgroundColor (0xfff59563);//1.1 This method stores the values of all properties of the declare-styleable that you defined in Atts.xml into TypedarrayTypedArray ta = context.obtainstyledattributes (attrs, R.styleable.topbar);//1.2 remove the corresponding value from the Typedarray to assign a value to the property you want to setMlefttextcolor = Ta.getcolor (R.styleable.topbar_lefttextcolor,0);        Mleftbackground = ta.getdrawable (R.styleable.topbar_leftbackground);        Mlefttext = ta.getstring (R.styleable.topbar_lefttext); Mrighttextcolor = Ta.getcolor (R.styleable.topbar_righttextcolor,0);        Mrightbackground = ta.getdrawable (R.styleable.topbar_rightbackground);        Mrighttext = ta.getstring (R.styleable.topbar_righttext); Mtitletextsize = Ta.getdimension (R.styleable.topbar_titletextsize,Ten); Mtitletextcolor = Ta.getcolor (R.styleable.topbar_titletextcolor,0); Mtitle = ta.getstring (R.styleable.topbar_title);//1.3 After you get the value of Typedarray, you typically call the Recyle method to avoid re-creating the errorTa.recycle ();//2.1 Get component ElementsMleftbutton =NewButton (context); Mrightbutton =NewButton (context); Mtitleview =NewTextView (context);//2.2 Set the appropriate layout element for the component elementMleftparams =NewLayoutparams (Layoutparams.wrap_content, layoutparams.match_parent);        Mleftparams.addrule (Relativelayout.align_parent_left, TRUE); Mrightparams =NewLayoutparams (Layoutparams.wrap_content, layoutparams.match_parent);        Mrightparams.addrule (Relativelayout.align_parent_right, TRUE); Mtitlepparams =NewLayoutparams (Layoutparams.wrap_content, layoutparams.match_parent); Mtitlepparams.addrule (Relativelayout.center_in_parent, TRUE);///3.1 assigns a value to the created component element, derived from the assignment of the corresponding property in the referenced Attr.xml fileMleftbutton.settextcolor (Mlefttextcolor);        Mleftbutton.setbackground (Mleftbackground);        Mleftbutton.settext (Mlefttext);        Mrightbutton.settextcolor (Mrighttextcolor);        Mrightbutton.setbackground (Mrightbackground);        Mrightbutton.settext (Mrighttext);        Mtitleview.settext (Mtitle);        Mtitleview.settextcolor (Mtitletextcolor);        Mtitleview.settextsize (mtitletextsize); Mtitleview.setgravity (Gravity.center);//4.1 Adding component elements to ViewGroupAddView (Mleftbutton, mleftparams);        AddView (Mrightbutton, mrightparams); AddView (Mtitleview, mtitlepparams);//4.2 Set button click event, do not need specific implementation, just call the interface method, callback, there will be a specific implementationMrightbutton.setonclicklistener (NewOnclicklistener () {@Override             Public void OnClick(View v)            {Mlistener.rightclick ();        }        }); Mleftbutton.setonclicklistener (NewOnclicklistener () {@Override             Public void OnClick(View v)            {Mlistener.leftclick ();    }        }); }/** * Set monitoring * *    //interface object that implements a callback mechanism that invokes methods in the interface through the mapped interface object in the callback method    //Instead of thinking about how it is implemented, the specific implementation is created by the caller     Public  interface topbarclicklistener {        //Left button click event        voidLeftclick ();//Right button click event        voidRightClick (); }//Map Incoming interface objects    PrivateTopbarclicklistener Mlistener;//Exposes a method to the caller to register an interface callback, through an interface to obtain the implementation of the interface method by the callback     Public void Setontopbarclicklistener(Topbarclicklistener Mlistener) { This. Mlistener = Mlistener; }/** * Sets whether the button is displayed or not by the ID-sensitive button, flag distinguishes if it is displayed * * @param ID ID * @param flag is displayed */     Public void setbuttonvisable(intIdBooleanFlag) {if(flag) {if(id = =0) {mleftbutton.setvisibility (view.visible); }Else{mrightbutton.setvisibility (view.visible); }        }Else{if(id = =0) {mleftbutton.setvisibility (view.gone); }Else{mrightbutton.setvisibility (View.gone); }        }    }}
2.4 Topbar_test.xml
<relativelayout xmlns:android="Http://schemas.android.com/apk/res/android" Xmlns:custom="Http://schemas.android.com/apk/res-auto"xmlns:tools="http// Schemas.android.com/tools "android:layout_width=" Match_parent "android:layout_height ="Match_parent"android:padding= "5dp"tools:context=". Mainactivity ">                            <!--<include layout= "@layout/topbar"/> --    <Com.imooc.systemwidget.TopBar        Android:id="@+id/topbar"        Android:layout_width="Match_parent"        Android:layout_height="40DP"        Custom:leftbackground="@drawable/blue_button"        Custom:lefttext="Back"        Custom:lefttextcolor="#FFFFFF"        Custom:rightbackground="@drawable/blue_button"        Custom:righttext="More"        Custom:righttextcolor="#FFFFFF"        Custom:title="Custom title"        Custom:titletextcolor="#123412"        custom:titletextsize="10SP"/></relativelayout>
2.5 Topbartest.java
 PackageCom.imooc.systemwidget; Public  class topbartest extends Activity {    PrivateTopbar Mtopbar;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (r.layout.topbar_test);//Get the Topbar we createdMtopbar = (Topbar) Findviewbyid (R.id.topbar);//Register listener events for Topbar, pass in the defined interface        //And implement the methods within the interface in an anonymous classMtopbar.setontopbarclicklistener (NewTopbar.topbarclicklistener () {@Override                     Public void RightClick() {Toast.maketext (topbartest. This,"Right", Toast.length_short). Show (); }@Override                     Public void Leftclick() {Toast.maketext (topbartest. This,"left", Toast.length_short). Show (); }                });//Control the status of components on TopbarMtopbar.setbuttonvisable (0,true); Mtopbar.setbuttonvisable (1,false); }}

Custom composite controls for Android Custom View--title bar

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.