Consistent style, first look, then realize
Write the custom properties file Atts.xml, the properties involved in the custom attribute have the left and right sides of the button's background, the contents of the middle title, font size, font color
<?XML version= "1.0" encoding= "Utf-8"?><Resources> <declare-styleablename= "Topbar"> <attrname= "Leftbackground"format= "Reference"/> <attrname= "Rightbackground"format= "Reference"/> <attrname= "TitleText"format= "string"/> <attrname= "Titletextsize"format= "Dimension"/> <attrname= "Titletextcolor"format= "Color|reference"/> </declare-styleable></Resources>
Write the layout file layout_topbar.xml, using the relative layout, a button on the left, left-aligned with the parent control, the margin is 5DP, the button on the right is the same, and the middle caption is centered
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Tools:context=". Topbar "> <ButtonAndroid:id= "@+id/leftbutton"Android:layout_width= "30DP"Android:layout_height= "30DP"Android:layout_alignparentleft= "true"android:layout_centervertical= "true"Android:layout_marginleft= "5DP" /> <TextViewAndroid:id= "@+id/titletextview"Android:layout_width= "Wrap_content"Android:layout_height= "Match_parent"Android:layout_centerhorizontal= "true"android:gravity= "Center" /> <ButtonAndroid:id= "@+id/rightbutton"Android:layout_width= "30DP"Android:layout_height= "30DP"Android:layout_alignparentright= "true"android:layout_centervertical= "true"Android:layout_marginright= "5DP" /></Relativelayout>
Write custom controls, inherit Relativelayout, get custom properties and assign values to corresponding controls
Public classTopbarextendsRelativelayout {PrivateButton LeftButton, Rightbutton; PrivateTextView Titletextview; PrivateOnleftandrightclicklistener Listener;//Monitor Click events//Setting up Listeners Public voidSetonleftandrightclicklistener (Onleftandrightclicklistener listener) { This. Listener =Listener; } //set the visibility of the left button Public voidSetleftbuttonvisibility (Booleanflag) { if(flag) leftbutton.setvisibility (VISIBLE); Elseleftbutton.setvisibility (INVISIBLE); } //set the visibility of the right button Public voidSetrightbuttonvisibility (Booleanflag) { if(flag) rightbutton.setvisibility (VISIBLE); Elserightbutton.setvisibility (INVISIBLE); } //button Click Interface Public InterfaceOnleftandrightclicklistener { Public voidOnleftbuttonclick (); Public voidOnrightbuttonclick (); } PublicTopbar (Context context, AttributeSet attrs) {Super(context, attrs); Layoutinflater.from (context). Inflate (R.layout.layout_topbar, This); LeftButton=(Button) Findviewbyid (R.id.leftbutton); Rightbutton=(Button) Findviewbyid (R.id.rightbutton); Titletextview=(TextView) Findviewbyid (R.id.titletextview); Leftbutton.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {if(Listener! =NULL) Listener.onleftbuttonclick ();//Click Callback } }); Rightbutton.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {if(Listener! =NULL) Listener.onrightbuttonclick ();//Click Callback } }); //get custom attributes and assign valuesTypedArray TypedArray =context.obtainstyledattributes (Attrs, R.styleable.topbar); intLeftbtnbackground = Typedarray.getresourceid (r.styleable.topbar_leftbackground, 0); intRightbtnbackground = Typedarray.getresourceid (r.styleable.topbar_rightbackground, 0); String TitleText=typedarray.getstring (R.styleable.topbar_titletext); floatTitletextsize = typedarray.getdimension (r.styleable.topbar_titletextsize, 0); intTitletextcolor = Typedarray.getcolor (R.styleable.topbar_titletextcolor, 0x38ad5a); Typedarray.recycle ();//Freeing ResourcesLeftbutton.setbackgroundresource (Leftbtnbackground); Rightbutton.setbackgroundresource (Rightbtnbackground); Titletextview.settext (TitleText); Titletextview.settextsize (titletextsize); Titletextview.settextcolor (Titletextcolor); }}
Invoking a custom control
<Cn.edu.zafu.view.topbar.TopBarAndroid:id= "@+id/topbar"Android:layout_width= "Match_parent"Android:layout_height= "50DP"Android:background= "#38ad5a"Custom:leftbackground= "@drawable/left_button_selector"Custom:rightbackground= "@drawable/right_button_selector"Custom:titletext= "Title content"Custom:titletextcolor= "#000"custom:titletextsize= "6sp" />
Code called in the activity
Public classMainactivityextendsactionbaractivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Topbar Topbar=(Topbar) Findviewbyid (R.id.topbar); Topbar.setonleftandrightclicklistener (NewTopbar.onleftandrightclicklistener () {@Override Public voidOnleftbuttonclick () {Toast.maketext (), Getapplicationcontext (),"Left", Toast.length_short). Show (); } @Override Public voidOnrightbuttonclick () {Toast.maketext (), Getapplicationcontext (),"Right", Toast.length_short). Show (); } }); Topbar.setleftbuttonvisibility (false); }}
The two selector used in the previous layout are as follows
Writing Left_button_selector.xml
<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android"> <Itemandroid:drawable= "@drawable/left_pressed"android:state_pressed= "true"/> <Itemandroid:drawable= "@drawable/left_normal" /></selector>
Writing Right_button_selector.xml
<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android"> <Itemandroid:drawable= "@drawable/right_pressed"android:state_pressed= "true"/> <Itemandroid:drawable= "@drawable/right_normal" /></selector>
android--Custom Control Optimizations