Consistent style, first look, then realize
Writing custom properties File Atts.xml, the properties involved in the custom attribute have the left and right sides of the button's background, the content of the middle title, the font size, the font color.
<?xml version= "1.0" encoding= "Utf-8"?><resources> <declare-styleable name= "Topbar" > <attr name= "Leftbackground" format= "reference"/> <attr name= "Rightbackground" format= "Reference"/> <attr name= "TitleText" format= "string"/> <attr name= "titletextsize" format= "Dimension"/> <attr name= "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
<relativelayout xmlns: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 "> <button android:id=" @+id/leftbutton "android:layout_width=" 30DP "Android:layout_hei ght= "30DP" android:layout_alignparentleft= "true" android:layout_centervertical= "true" Android:layout_ marginleft= "5DP"/> <textview android:id= "@+id/titletextview" android:layout_width= "Wrap_content" android:layout_height= "Match_parent" android:layout_centerhorizontal= "true" android:gravity= "center"/ > <button android:id= "@+id/rightbutton" android:layout_width= "30DP" android:layout_height= "3 0DP "android:layout_alignparentright=" true "android:layout_centervertical=" true "Android:layout_margi nright= "5DP"/></relativelayout>
Write custom controls, inherit Relativelayout, get custom properties and assign values to corresponding controls
Package Cn.edu.zafu.view.topbar;import Android.content.context;import Android.content.res.typedarray;import Android.util.attributeset;import Android.view.layoutinflater;import Android.view.view;import Android.widget.button;import android.widget.relativelayout;import android.widget.textview;/** * Created by Lizhangqu On 2015/1/18. */public class Topbar extends Relativelayout {private Button LeftButton, Rightbutton; Private TextView Titletextview; Private Onleftandrightclicklistener listener;//Monitor Click event/Set Listener public void Setonleftandrightclicklistener (Onleftandri Ghtclicklistener listener) {This.listener = listener; }//Set the visibility of the left button public void Setleftbuttonvisibility (Boolean flag) {if (flag) Leftbutton.setvisibilit Y (VISIBLE); else leftbutton.setvisibility (INVISIBLE); }//Set the visibility of the right button public void Setrightbuttonvisibility (Boolean flag) {if (flag) Rightbutton.setvisibil ity (VISIBLE); Else Rightbutton.setvisibility (INVISIBLE); }//button Click Interface public interface Onleftandrightclicklistener {public void Onleftbuttonclick (); public void Onrightbuttonclick (); } public Topbar (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 (New Onclicklistener () {@Override public void OnClick (View v) { if (listener! = null) Listener.onleftbuttonclick ();//Click Callback}}); Rightbutton.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) { if (listener! = null) Listener.onrightbuttonclick ();//Click Callback } }); Get custom attributes and assign values TypedArray TypedArray = Context.obtainstyledattributes (Attrs, R.styleable.topbar); int leftbtnbackground = Typedarray.getresourceid (r.styleable.topbar_leftbackground, 0); int rightbtnbackground = Typedarray.getresourceid (r.styleable.topbar_rightbackground, 0); String TitleText = typedarray.getstring (R.styleable.topbar_titletext); float titletextsize = typedarray.getdimension (r.styleable.topbar_titletextsize, 0); int titletextcolor = Typedarray.getcolor (R.styleable.topbar_titletextcolor, 0X38AD5A); Typedarray.recycle ();//release of resources Leftbutton.setbackgroundresource (Leftbtnbackground); Rightbutton.setbackgroundresource (Rightbtnbackground); Titletextview.settext (TitleText); Titletextview.settextsize (titletextsize); Titletextview.settextcolor (Titletextcolor); }}
Invoking a custom control
<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" tools:context= ". Mainactivity "> <cn.edu.zafu.view.topbar.topbar android: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"/></relativelayout>
Package Cn.edu.zafu.view.topbar;import Android.os.bundle;import Android.support.v7.app.actionbaractivity;import Android.widget.toast;public class Mainactivity extends Actionbaractivity { @Override protected void OnCreate ( Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Topbar topbar= (Topbar) Findviewbyid (R.id.topbar); Topbar.setonleftandrightclicklistener (New Topbar.onleftandrightclicklistener () { @Override public Void Onleftbuttonclick () {Toast.maketext () { getapplicationcontext (), "left", Toast.length_short). Show (); @Override public void Onrightbuttonclick () { 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"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:drawable=" @drawable/left_pressed "android:state_pressed=" true "/> < Item android:drawable= "@drawable/left_normal"/></selector>
Writing Right_button_selector.xml
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:drawable=" @drawable/right_pressed "android:state_pressed=" true "/> < Item android:drawable= "@drawable/right_normal"/></selector>
Android custom Control--Reuse of title bar