<span id="Label3"></p><p><p>Write in Front:</p></p><p><p>When we write applications, we often find that many interfaces have the same or similar parts, such as the same topbar or the same bottom.</p></p><p><p>We take QQ as an example:</p></p><p><p><span style="color:#ff0000"></span></p></p><p><p>And these similar parts once modified, is all the changes together, if you change one or two is ok, more you try? It is said that laziness is the ladder of programmer progress, so lazy programmers use the <strong>template</strong> to improve their productivity, but <strong></strong> also improve the readability of the code <strong>, maintainability</strong> . This article will be with you to achieve our own template, a topbar. The effect is as Follows:</p></p><p><p><span style="color:rgb(255,0,0)"></span></p></p><p><p>The perfectly formed, the important thing is the thought.</p></p><p><p><br></p></p>After reading this article, you can learn:<p><p><span style="font-size:18px">Use of 1.declare-styleable</span></p></p><p><p><span style="font-size:18px">2. Custom Controls</span></p></p><p><p><span style="font-size:18px">3. Reusing a custom control in the interface</span></p></p><p><p><br></p></p>one, Custom Controls<br>Now what we're going to do is a topbar, as we can see it is made up of a left button, a title, a right button, and we define a layout class produced by these three parts:<p><p></p></p><pre name="code" class="java">public class Topbar extends relativelayout {private button leftbutton, rightbutton;private TextView title;//left Button Property pr ivate int lefttextcolor;private drawable leftbackground;private String lefttext;//right button Property private int Righttextcolor ;p rivate drawable rightbackground;private String righttext;//title Property private int titletextcolor;private float Titletextsize;private String titletext;//layout Properties Private layoutparams leftparams, rirhtparams, titleparams;private Topbarclicklistener listener; Public Topbar (context context, attributeset attrs) {super (context, Attrs);//TODO auto-generated constructor Stub//this method is still empty, the following will be populated! }//click Event Listener Interface public interface Topbarclicklistener {public void Leftclick ();p ublic void RightClick ();} Set listener public void Setontopbarclicklistener (topbarclicklistener listener) {this.listener = listener;} public void setleftisvisible (boolean visible) {if (visible) {leftbutton.setvisibility (view.visible);} else { Leftbutton.setvisibility (view.gone);}} public void Setrightisvisible (booleanVisible) {if (visible) {rightbutton.setvisibility (view.visible);} Else {rightbutton.setvisibility (view.gone);}}} </pre><p><p>As you can see, we define the three required controls and the properties of the three controls in this Layout. We have also set up an interface callback mechanism, which can be used to set the listening mechanism by using the other layouts of this custom Control. You can also call the Setisvisible method to hide or show the buttons on the left and right Sides. But now we haven't assigned values for these properties, so let's keep looking.</p></p><p><p></p></p><p><p><br></p></p>second, use declare-styleable to define attributes<p><p>What is declare-styleable? is used to define the property set in Attrs.xml under the values Folder.</p></p><p><p></p></p><p style="margin:5px auto"><p style="margin:5px auto">First look at the Attrs.xml File.</p></p><p style="margin:5px auto"><p style="margin:5px auto">This file is where you define the property name and format, and you need to <declare-styleable name= "topbar" ></declare-styleable> surround all the Attributes. Where name is the name of the property set, the primary purpose is to identify the property set. Where is it going to be used? When our custom control gets a property id, it uses "r.styleable.topbar ..." (see later), and it is clear that he added "topbar_" to each Property.</p></p><p style="margin:5px auto"><p style="margin:5px auto">Look at the types of properties: string, integer, dimension, reference, color, enum ...</p></p><p style="margin:5px auto"><p style="margin:5px auto">The preceding declarations are consistent, for example: <attr name= "buttonnum" format= "integer"/>.<br>Only the enum is different, use an example:</p></p><p style="margin:5px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><p style="margin:5px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"></p></p><pre name="code" class="html"><pre name="code" class="html"><span style= "color: #330000;" ><attr name= "testenum" > <enum name= "fill_parent" value= "-1"/> <enum name= "wrap_content" Value= "-2"/> </attr></span></pre></pre><p><p></p></p><p style="margin:5px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><p style="margin:5px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">Now we should understand the use and wording of attrs.xml, and we will write a attrs.xml for the custom Topbar (put it in the values folder):</p></p><p style="margin:5px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><p style="margin:5px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"></p></p><pre name="code" class="html"><pre name="code" class="html"><?xml version= "1.0" encoding= "utf-8"? ><resources><declare-styleable name= "topbar" ><!--text-- ><attr name= "lefttext" format= "string"/><attr name= "righttext" format= "string"/><attr name= " TitleText "format=" string "/><!--text color--><attr name=" lefttextcolor "format=" color "/><attr name=" Righttextcolor "format=" color "/><attr name=" titletextcolor "format=" color "/><!--text size--><attr Name= "titletextsize" format= "dimension"/><!--background color--><attr name= "leftbackground" format= "reference| Color "/><attr name=" rightbackground "format=" reference|color "/></declare-styleable></resources ></pre></pre><p style="margin:5px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px"><p style="margin:5px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px">We then add the constructor for the Topbar class Above:</p></p><pre name="code" class="java">Public Topbar (context context, attributeset attrs) {super (context, attrs);//gets A collection of custom properties and values for the map Typedarray ta = Context.obtainstyledattributes (attrs, r.styleable.topbar);//remove the custom Attribute-left Lefttextcolor = Ta.getcolor ( r.styleable.topbar_lefttextcolor, color.black); leftbackground = ta.getdrawable (r.styleable.topbar_leftbackground) ; lefttext = ta.getstring (r.styleable.topbar_lefttext);//remove custom Properties-right righttextcolor = Ta.getcolor (r.styleable.topbar_ righttextcolor, color.black); rightbackground = ta.getdrawable (r.styleable.topbar_rightbackground); RightText = ta.getstring (r.styleable.topbar_righttext);//remove Custom Attribute-title titletextcolor = Ta.getcolor (r.styleable.topbar_ titletextcolor, color.black); titletextsize = ta.getdimension (r.styleable.topbar_titletextsize, n); titleText = Ta.getstring (r.styleable.topbar_titletext);//recycle Typedarray (avoid wasting resources and avoid errors caused by Caching) ta.recycle (); LeftButton = new Button Rightbutton = new Button (context), title = new TextView (context);//set property-left leftbutton.settext (lefttext); leftbutton.seTtextcolor (lefttextcolor); leftbutton.setbackground (leftbackground);//set properties-left rightbutton.settext (rightText); Rightbutton.settextcolor (righttextcolor); rightbutton.setbackground (rightbackground);//set properties-title title.settext ( titletext); title.settextsize (titletextsize); title.settextcolor (titletextcolor); title.setgravity (Gravity.CENTER )///set The overall background color SetBackgroundColor (0xfff59563);//set layout-left leftparams = new Layoutparams (layoutparams.wrap_content, layoutparams.wrap_content); leftparams.addrule (relativelayout.align_parent_left, TRUE); leftParams.addRule ( relativelayout.center_vertical, TRUE); addview (leftbutton, leftparams);//add button to layout//set layout-right rirhtparams = new Layoutparams (layoutparams.wrap_content, layoutparams.wrap_content); rirhtparams.addrule (RelativeLayout.ALIGN_ parent_right, true); rirhtparams.addrule (relativelayout.center_vertical, true); addview (rightbutton, rirhtParams); /add button into Layout//set layout-title titleparams = new Layoutparams (layoutparams.wrap_content, layoutparams.wrap_content); Titleparams.addrule (Relativelayout.center_in_parent, TRUE); addview (title, titleparams);//add button to layout// Set listener Leftbutton.setonclicklistener (new onclicklistener () {@Overridepublic void OnClick (View v) {listener.leftclick () ;}}); Rightbutton.setonclicklistener (new onclicklistener () {@Overridepublic void OnClick (View v) {listener.rightclick ();}});}</pre>This constructor is used to assign values to the Control's properties, and also to listen to the control and add it to its own layout.<br><br>third, Use our custom controls in other layouts<p><p></p></p><p><p></p></p><p><p>We're going to use this topbar in mainactivity, so we'll start by referencing our custom controls in its layout file (activity_main.xml):</p></p><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"><relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " xmlns:app=" Http://schemas.android.com/apk/res/com.sloop.topbar " android: Layout_width= "match_parent" android:layout_height= "match_parent" tools:context= " Com.sloop.topbar.MainActivity "> <com.example.mytopbar.topbar android:minheight=" 48dp " android:id= "@+id/topbar" android:layout_width= "match_parent" android:layout_height= "wrap_content" app:leftbackground= "@drawable/blue_button" app:lefttext= "left" app:titletextsize= "8sp" app: rightbackground= "@drawable/blue_button" app:righttext= "right" app:titletext= "custom title" app: titletextcolor= "#ffffff" > </com.example.mytopbar.TopBar></RelativeLayout></pre></pre>In this file we assign values to the properties of its three components, and the attentive friend may find that the background of the button is using a custom blue_button, the following is Blue_button.xml (under the drawable folder):<p><p></p></p><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"><?xml version= "1.0" encoding= "utf-8"?><!--shape If you do not declare the shape by default is Square--><shape xmlns:android= "http/ schemas.android.com/apk/res/android > <!--set rounded corners- <corners android:radius= "2dp" > </corners> <!--to Set the fill color- <solid android:color= #4285f4 > </solid> <!- -set the color and width of the border-- <stroke android:width= "1dp" android:color= "#4285f4" > </stroke> </shape></pre></pre><p><p>To grow up, you can modify your needs according to your Requirements.</p></p>Then there is the code for mainacticity:<p><p></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);//get The custom control Topbar Topbar = (topbar) Findviewbyid ( r.id.topbar); The ID is the Topbar.setontopbarclicklistener defined in Acitivity_main (new topbarclicklistener () {@Overridepublic void RightClick () {toast.maketext (mainactivity.this, "right Clicked", toast.length_short). show (); @Overridepublic void Leftclick () {toast.maketext (mainactivity.this, "left Clicked", toast.length_short). show ();}});}}</pre></pre><p><p>The interface defined in Topbar is implemented to add different listeners according to different Requirements. You can also call the SetVisible method to control whether the display is Displayed.</p></p><p><p>here, we have implemented our custom topbar and achieved a high degree of Reusability.</p></p><p><p>The effect is as Follows:</p></p><p><p><span style="color:#ff0000"></span></p></p><p><p><br></p></p><p><p><br></p></p><p><p></p></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">========================================</p></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Write in the Back:</p></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">The source code has been uploaded to my github, or downloaded to the CSDN download Area.</p></p><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Any questions, welcome message exchange!</p></p><br><p><p></p></p><p><p><br></p></p><br><p><p></p></p><p><p>Android easy to design UI template</p></p></span>
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