The examples in this article describe the implementation of the Android custom component. Share to everyone for your reference. Specifically as follows:
Atts.xml:
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<declare-styleable name= "Topbar" >
<attr name= "TitleText" format= "string"/> <attr name=
"titletextsize" format= "Dimension"/>
<attr name= "Titletextcolor" format= "color"/> <attr name=
"Lefttext" format= "string"/>
<attr Name= "Leftbackground" format= "Reference|color"/> <attr name= "Lefttextcolor"
format= "color"/>
<attr name= "Righttext" format= "string"/> <attr name=
"Rightbackground" format= "Reference|color"/>
<attr name= "righttextcolor" format= "color"/>
</declare-styleable>
</resources>
Topbar.java:
Package Com.cd.administrator.mytopbar;
Import Android.annotation.TargetApi;
Import Android.content.Context;
Import Android.content.res.TypedArray;
Import android.graphics.drawable.Drawable;
Import Android.os.Build;
Import Android.util.AttributeSet;
Import android.view.Gravity;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.Button;
Import Android.widget.RelativeLayout;
Import Android.widget.TextView;
Import Android.widget.Toast;
/** * Created by the Administrator on 2015/1/8.
* * public class Topbar extends relativelayout{private Button Leftbutton,rightbutton;
Private TextView Tvtitle;
private int lefttextcolor;
Private drawable Leftbackground;
Private String Lefttext;
private int righttextcolor;
Private drawable Rightbackground;
Private String Righttext;
private int titletextcolor;
Private String TitleText;
private float titletextsize;
Private Layoutparams Leftparams,rightparams,titleparams;
Private Topbarclicklistener listener; public interface topbarclicklistener{public void Leftclick ();
public void RightClick ();
public void Setontopbarclicklistener (Topbarclicklistener listener) {This.listener = listener; } @TargetApi (Build.version_codes.
Jelly_bean) Public Topbar (final context, AttributeSet Attrs) {Super (context, attrs);
TypedArray ta = context.obtainstyledattributes (Attrs,r.styleable.topbar);
Lefttextcolor = Ta.getcolor (r.styleable.topbar_lefttextcolor,0);
Leftbackground = ta.getdrawable (R.styleable.topbar_leftbackground);
Lefttext = ta.getstring (R.styleable.topbar_lefttext);
Righttextcolor = Ta.getcolor (r.styleable.topbar_righttextcolor,0);
Rightbackground = ta.getdrawable (R.styleable.topbar_rightbackground);
Righttext = ta.getstring (R.styleable.topbar_righttext);
Titletextcolor = Ta.getcolor (r.styleable.topbar_titletextcolor,0);
Titletextsize = Ta.getdimension (r.styleable.topbar_titletextsize,0); TitleText = ta.getstring (r.styLeable.
Topbar_titletext);
Ta.recycle ();
LeftButton = new Button (context);
Rightbutton = new Button (context);
Tvtitle = new TextView (context);
Leftbutton.settextcolor (Lefttextcolor);
Leftbutton.setbackground (Leftbackground);
Leftbutton.settext (Lefttext);
Rightbutton.settextcolor (Righttextcolor);
Rightbutton.setbackground (Rightbackground);
Rightbutton.settext (Righttext);
Tvtitle.settextcolor (Titletextcolor);
Tvtitle.settextsize (titletextsize);
Tvtitle.settext (TitleText);
Tvtitle.setgravity (Gravity.center);
SetBackgroundColor (0xf59563);
Leftparams = new Layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Leftparams.addrule (relativelayout.align_parent_left,true);
AddView (Leftbutton,leftparams);
Rightparams = new Layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Rightparams.addrule (relativelayout.align_parent_right,true); AddView (rightButton,rightparams);
Titleparams = new Layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
Titleparams.addrule (relativelayout.center_in_parent,true);
AddView (Tvtitle,titleparams); Leftbutton.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {Listener
. Leftclick ();
}
}); Rightbutton.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {Listene
R.rightclick ();
}
});
}
}
Activity_main.xml:
<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 "android:paddingleft=" @dimen/activity_horizontal_margin "Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" Android: paddingbottom= "@dimen/activity_vertical_margin" tools:context= ". Mainactivity "> <com.cd.administrator.mytopbar.topbar android:id=" @+id/topbar "android:layout_width=" Match_ Parent "android:layout_height=" 40DP "custom:leftbackground=" "@drawable/blue" custom:lefttext= "Back" custom: Lefttextcolor= "#ffffff" custom:rightbackground= "@drawable/blue" custom:righttext= "more" custom:righttextcolor= "#ffffff" custom:titletextcolor= "#121212" custom:titletextsize= "15sp" custom:titletext= "custom title" > </com. Cd.aDministrator.mytopbar.topbar> </RelativeLayout>
Mainactivity.java:
Package Com.cd.administrator.mytopbar;
Import android.support.v7.app.ActionBarActivity;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;
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.setontopbarclicklistener (New Topbar.topbarclicklistener () {@Override public void Leftclick () {
Toast.maketext (Mainactivity.this, "Cd--left", Toast.length_short). Show (); @Override public void RightClick () {Toast.maketext (mainactivity.this, "Cd--right", Toast.length_short
). Show ();
}
}); @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action Bar
If it is present. Getmenuinflater (). Inflate (R.menu.menu_main, menu);
return true; @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar would//automatically handle clicks on the Home/up button, so long//As you specify a parent Activ
ity in Androidmanifest.xml.
int id = item.getitemid ();
Noinspection simplifiableifstatement if (id = = r.id.action_settings) {return true;
return super.onoptionsitemselected (item);
}
}
I hope this article will help you with your Android program.