Android combines multiple controls to implement custom title bars

Source: Internet
Author: User
Tags bind xmlns

Here we use the classic left-right layout, can also be adjusted to the needs of the project, such as the right to add a control, or to the left side of the title can be

Note: It is applied to the BUTTONM control in the previous article, you can get a brief look at it first.

Let's take a look at the effect chart:







Here is the code implementation, a total of four files two Java classes, two layout files





1 Titlebarm.java








Package Landptf.control;





Import Android.content.Context;


Import Android.graphics.Color;


Import Android.util.AttributeSet;


Import Android.util.TypedValue;


Import Android.view.View;


Import Android.widget.RelativeLayout;


Import Android.widget.TextView;


/**


* Inherit relativelayout, customize title bar


* @author LANDPTF


* @date 2015-6-10


*/


public class Titlebarm extends relativelayout{


private context;


Defines three controls,


Private Buttonm Btnleft;


Private TextView Tvtitle;


Private Buttonm btnright;


Defines the interface of the left control


Private Onclicklistenerl Onclicklistenerl = null;


Defines the interface of the right control


Private Onclicklistenerr onclicklistenerr = null;





public interface onclicklistenerl{


Define a method


public void OnClick (View v);


}





public interface onclicklistenerr{


Define a method


public void OnClick (View v);


}





/**


* Bind event for left control


* @param Onclicklistenerl


*/


public void Setonclicklisenerl (Onclicklistenerl onclicklistenerl) {


This.onclicklistenerl = Onclicklistenerl;


}





/**


* Bind event for right control


* @param onclicklistenerr


*/


public void Setonclicklisenerr (Onclicklistenerr onclicklistenerr) {


This.onclicklistenerr = Onclicklistenerr;


}





Public Titlebarm {


This is (context, NULL);


}





Public Titlebarm (context context, AttributeSet Attrs) {


This is (context, attrs,0);


}





Public Titlebarm (context context, AttributeSet attrs, int defstyle) {


Super (context, attrs, Defstyle);


This.context = context;


Set the layout of the Relativelayout


Setlayoutparams (New Layoutparams (layoutparams.match_parent,layoutparams.match_parent));


Set the default background color


SetBackgroundColor (Color.parsecolor ("#B674D2"));


Init ();


}





private void init () {


Initialize left Buttonm


Btnleft = new Buttonm (context);


Relativelayout.layoutparams lp=new Relativelayout.layoutparams (layoutparams.wrap_content,layoutparams.wrap_ CONTENT);


Center vertically


Lp.addrule (relativelayout.center_vertical);


Set the distance to the left 10DP


Lp.leftmargin = dp2px (context, 10);


BTNLEFT.SETLAYOUTPARAMS (LP);


Btnleft.settextsize (16);//Set font size, default to 16


Btnleft.settextcolori (Color.White);//default font color is white


Btnleft.settextcolorselected ("#909090");//pressed font Color


Define its click events


Btnleft.setonclicklistener (New Onclicklistener () {


@Override


public void OnClick (View v) {


if (Onclicklistenerl!= null) {


Onclicklistenerl.onclick (v);


}


}


});


Initializing an intermediate caption control


Tvtitle = new TextView (context);


Lp=new Relativelayout.layoutparams (layoutparams.wrap_content,layoutparams.wrap_content);


Place it in the middle of the parent control, and some of the app's titles are left, can be adjusted to suit the needs of the project, and can be set dynamically


Lp.addrule (relativelayout.center_in_parent);


TVTITLE.SETLAYOUTPARAMS (LP);


Set Caption text color


Tvtitle.settextcolor (Color.White);


Set caption Text size


Tvtitle.settextsize (18);//default is 18


Initialize Right Buttonm


Btnright = new Buttonm (context);


Lp=new Relativelayout.layoutparams (layoutparams.wrap_content,layoutparams.wrap_content);


Lp.rightmargin = dp2px (context, 10);


Center vertically


Lp.addrule (relativelayout.center_vertical);


To the right of the parent control


Lp.addrule (Relativelayout.align_parent_right);


BTNRIGHT.SETLAYOUTPARAMS (LP);


Btnright.settextsize (16);//default has 16


Btnright.setvisibility (View.gone); Hide right control by default


Btnright.settextcolori (Color.White);


Btnright.settextcolorselected ("#909090");


Btnright.setonclicklistener (New Onclicklistener () {


@Override


public void OnClick (View v) {


if (Onclicklistenerr!= null) {


Onclicklistenerr.onclick (v);


}


}


});





Add three controls to the container individually


AddView (Btnleft);


AddView (Tvtitle);


AddView (Btnright);


}





/**


* Set the background color of the title bar, String


* @param backcolors


*/


public void Setbackcolor (String backcolors) {


SetBackgroundColor (Color.parsecolor (backcolors));


}





/**


* Set the background color of the title bar, int


* @param Backcolori


*/


public void Setbackcolor (int backcolori) {


SetBackgroundColor (Backcolori);


}





/**


* Set the text displayed on the left control


* @param lefttext


*/


public void Setlefttext (String lefttext) {


Btnleft.settext (Lefttext);


}





/**


* Set the background image of the left control


* @param leftbackimage


*/


public void setleftbackimage (int leftbackimage) {


if (leftbackimage!= 0) {


Btnleft.setbackgroundimage (Leftbackimage);


}


}





/**


* Set the background image selected by the left control


* @param leftbackimageseleted


*/


public void setleftbackimageseleted (int leftbackimageseleted) {


if (leftbackimageseleted!= 0) {


Btnleft.setbackgroundimageseleted (leftbackimageseleted);


}


}





/**


* Set left control display properties, default to display


* @param leftvisible


*/


public void Setleftvisible (Boolean leftvisible) {


Btnleft.setvisibility (leftvisible?) View.VISIBLE:View.GONE);


}





/**


* Set the font size displayed by the left control


* @param lefttextsize


*/


public void Setlefttextsize (float lefttextsize) {


Btnleft.settextsize (lefttextsize);


}





/**


* Set the intermediate control display properties, default to display


* @param leftvisible


*/


public void Settitlevisible (Boolean titlevisible) {


Tvtitle.setvisibility (titlevisible?) View.VISIBLE:View.GONE);


}





/**


* Set the text of the middle control


* @param titletext


*/


public void Settitletext (String titletext) {


Tvtitle.settext (TitleText);


}





/**


* Set the size of the middle control's text


* @param titletextsize


*/


public void Settitletextsize (float titletextsize) {


Tvtitle.settextsize (titletextsize);


}





/**


* Set the text displayed by the right control


* @param lefttext


*/


public void Setrighttext (String righttext) {


Btnright.settext (Righttext);


}





/**


* Set the background image of the right control


* @param leftbackimage


*/


public void setrightbackimage (int rightbackimage) {


if (rightbackimage!= 0) {


Btnright.setbackgroundimage (Rightbackimage);


}


}





/**


* Set the background image selected by the right control


* @param leftbackimageseleted


*/


public void setrightbackimageseleted (int rightbackimageseleted) {


if (rightbackimageseleted!= 0) {


Btnright.setbackgroundimageseleted (rightbackimageseleted);


}


}





/**


* Set the font size displayed by the right control


* @param lefttextsize


*/


public void Setrighttextsize (float righttextsize) {


Btnright.settextsize (righttextsize);


}





/**


* Set the right control display properties, the default is hidden


* @param leftvisible


*/


public void Setrightvisible (Boolean rightvisible) {


Btnright.setvisibility (rightvisible?) View.VISIBLE:View.GONE);


}





Define a private method dp2px


private int dp2px (context, float dpval) {


return (int) typedvalue.applydimension (Typedvalue.complex_unit_dip,


Dpval, Context.getresources (). Getdisplaymetrics ());


}


}











Some of the public methods defined above can be extended or reduced according to specific project requirements.





2 Child_titlebarm.xml Here the title bar is written as a separate layout file, which is directly available in other layout files














<?xml version= "1.0" encoding= "Utf-8"?>


<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"


Android:layout_width= "Match_parent"


android:layout_height= "50DP"


android:orientation= "Vertical" >





<landptf.control.titlebarm


Android:id= "@+id/tbm_title"


Android:layout_width= "Match_parent"


android:layout_height= "Match_parent" >


</landptf.control.TitleBarM>





</LinearLayout>











The title bar here defaults to 50DP





3 Activity_titlebar.xml








<?xml version= "1.0" encoding= "Utf-8"?>


<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= "Match_parent"


Tools:ignore= "Hardcodedtext" >





<include


Android:id= "@+id/title"


layout= "@layout/child_titlebarm"/>





<textview


Android:id= "@+id/tv_content"


Android:layout_width= "Wrap_content"


android:layout_height= "Wrap_content"


Android:layout_centerinparent= "true"


android:text= "Test title bar"


Android:textsize= "20sp"/>





</RelativeLayout>











It's simpler to include a child_titlebarm.xml, and define a textview without special instructions.


4 Titlebarmactivity.java








Package Landptf.control;





Import Landptf.control.TitleBarM.OnClickListenerL;


Import Landptf.control.TitleBarM.OnClickListenerR;


Import android.app.Activity;


Import Android.os.Bundle;


Import Android.view.View;


Import Android.widget.Toast;





/**


* Titlebarm Test class


* @author LANDPTF


* @date 2015-6-10


*/


public class Titlebarmactivity extends activity{


Private Titlebarm Tbtitle;





@Override


protected void OnCreate (Bundle savedinstancestate) {


Super.oncreate (savedinstancestate);


Setcontentview (R.layout.activity_titlebarm);


Initview ();


}





private void Initview () {


Initializing controls


Tbtitle = (titlebarm) Findviewbyid (r.id.tbm_title);


First set a background color, of course, you can also use the default background color


Tbtitle.setbackcolor ("#7067E2");


Set the text displayed on the left, or you can set the background image


Tbtitle.setlefttext ("return");


Set left control Click event


Tbtitle.setonclicklisenerl (New Onclicklistenerl () {


@Override


public void OnClick (View v) {


Toast.maketext (Titlebarmactivity.this, "You clicked Back", Toast.length_short). Show ();


}


});


Set the middle caption


Tbtitle.settitletext ("LANDPTF");


Because we're hiding the controls on the right by default, this shows


Tbtitle.setrightvisible (TRUE);


Sets the background of the control on the right, and here you find two pictures of the search


Tbtitle.setrightbackimage (R.drawable.title_search);


Tbtitle.setrightbackimageseleted (r.drawable.title_search_selected);


Tbtitle.setonclicklisenerr (New Onclicklistenerr () {


@Override


public void OnClick (View v) {


Toast.maketext (Titlebarmactivity.this, "You clicked the Search", Toast.length_short). Show ();


}


});


}


}











This is all the code, which uses the Buttonm control described in the previous article, and you can use other controls to implement it as you want.





Here only provides a way of thinking, the lack of mutual exchange of learning.

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.