Android Custom Layouts

Source: Internet
Author: User
Tags getcolor

1. Create a new property file first
<?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= "Lefttext" format= "string"/> <attr        name= " Lefttextbackgroud "  format=" Reference|color "/>        <attr name=" lefttextcolor "format=" Color "/>                 <attr name= "Righttext" format= "string"/>        <attr name= "Righttextbackgroud"  format= "reference| Color "/>        <attr name=" righttextcolor "format=" color "/>    </declare-styleable></ Resources>


2. Create a custom layout class
public class Topbar extends Relativelayout {//custom control private Button LeftButton, Rightbutton;private TextView tvtitle;// Custom properties Private int lefttextcolor;private drawable leftbackground;private String lefttext;private int righttextcolor; Private drawable rightbackground;private string righttext;private string titletext;private float titletextsize;private int titletextcolor;private topbarlistener tlistener;public void Settlistener (Topbarlistener tListener) { This.tlistener = Tlistener;} public interface topbarlistener{public void Clickleft ();p ublic void Clickright (); Private Layoutparams Leftparams,rightparams,titleparams; @SuppressLint ("Newapi") public Topbar (context context, AttributeSet attrs) {Super (context, attrs);//TODO auto-generated constructor stub//Get property value Typedarray ta = Context.obtainstyledattributes (Attrs, r.styleable.topbar); lefttextcolor = Ta.getcolor (R.styleable.topbar_ Lefttextcolor, 0); leftbackground = Ta.getdrawable (r.styleable.topbar_lefttextbackgroud); leftText = Ta.getString ( R.stYleable.topbar_lefttext); Righttextcolor = Ta.getcolor (r.styleable.topbar_righttextcolor, 0); RightBackGround = Ta.getdrawable (r.styleable.topbar_righttextbackgroud); righttext = ta.getstring (R.styleable.topbar_righttext); TitleText = ta.getstring (r.styleable.topbar_title); titletextsize = Ta.getdimension (r.styleable.topbar_ Titletextsize, 0); Titletextcolor = Ta.getcolor (r.styleable.topbar_titletextcolor, 0); ta.recycle ();// Initializes the control LeftButton = New button (context), Rightbutton = New button (context), Tvtitle = new TextView (context); Leftbutton.settext (Lefttext); Leftbutton.setbackground (Leftbackground); Leftbutton.settextcolor (LeftTextColor); Rightbutton.settext (Righttext); Rightbutton.settextcolor (Righttextcolor); Rightbutton.setbackground ( Rightbackground); Tvtitle.settext (TitleText); tvtitle.settextsize (titletextsize); Tvtitle.settextcolor ( Titletextcolor); tvtitle.setgravity (Gravity.center); SetBackgroundColor (0xfff5346); leftparams = new LayoutParams ( Viewgroup.layoutparams.wrap_content,layoutparams.wrap_conTENT); Leftparams.addrule (relativelayout.align_parent_left,true); AddView (LeftButton, leftparams); rightParams = new Layoutparams (viewgroup.layoutparams.wrap_content,layoutparams.wrap_content); Rightparams.addrule ( Relativelayout.align_parent_right,true); AddView (Rightbutton, rightparams); titleparams = new LayoutParams ( viewgroup.layoutparams.wrap_content,layoutparams.match_parent); Titleparams.addrule (RelativeLayout.CENTER_IN_ Parent,true); AddView (Tvtitle, titleparams); Leftbutton.setonclicklistener (new View.onclicklistener () {@ overridepublic void OnClick (View arg0) {//TODO auto-generated method Stubtlistener.clickleft ();}}); Rightbutton.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO Auto-generated method Stubtlistener.clickright ();}});}}


3. Create a listener event and use
public class Mainactivity extends Activity {topbar top  ; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); top = (Topbar) Findviewbyid (R.id.myTop ); Top.settlistener (new Topbarlistener () {@Overridepublic void Clickright () {//TODO auto-generated method Stubtoast.maketext (Getapplicationcontext (), "clicked the right Button!", "()." Show (); @Overridepublic void Clickleft () {//TODO auto-generated method Stubtoast.maketext (Getapplicationcontext (), "clicked The left button! ", +). Show ();}}); @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:custom= "http// Schemas.android.com/apk/res/com.example.topbardemo "android:layout_width=" Match_parent "android:layout_height=" Match_parent "android:paddingbottom=" @dimen/activity_vertical_margin "android:paddingleft=" @dimen/activity_ Horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_ Vertical_margin "> <com.example.topbardemo.topbar android:id=" @+id/mytop "Android:layout_width=" mat Ch_parent "android:layout_height=" 40DP "custom:lefttext=" left "custom:lefttextcolor=" #003300 "CUSTOM:LEFTTEXTB Ackgroud= "#00BFFF" custom:title= "title" custom:titletextcolor= "#000000" custom:titletextsize= "14SP" Custom:rig Httext= "Right" custom:righttextcolor= "#000000" custom:righttextbackgroud= "#00BFFF" > </com.example.topbarde Mo. Topbar></relativelayout>



Some notes:1, in use, need to customize the reference value, xmlns:custom= "http://schemas.android.com/apk/res/xxx" XXX is the project package name2, the use of Typedarray need to release resources ta.recycle ();
:

Android Custom Layouts

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.