This article is for the Andbase framework to learn the first note, want to understand the andbase framework of friends can read this article, we learn together.
1. Use Andbase to implement the multi-function title bar
The Andbase framework provides many ways to enable us to set up a better title bar for dynamic change rather than statically writing the title bar interface to death ... Can make the title bar more beautiful ... The overall thing is to get the layout dynamically and then customize a good title bar by manipulating the layout ...
When using the Andbase framework, our main function is not inherited from Acticity, but inherits from Abactivity, same or activity ...
public class Mainactivity extends abactivity
Inherited the abactivity so that their activity has a framework ... We can use some of the variables that are contained inside ... Take our title bar variable ... mabtitlebar is an inner title bar variable ... An activity that inherits Abactivity can use this variable as a title bar variable in the current activity. We can directly make some of the relevant properties of the setting. So that we can write our own title bar ...
1.1 Set the title bar ...
The title bar has no setting is actually set the current Mabtitlebar whether to display ... By calling the Setvisiable function to make the related settings ...
public void Setvisiable (Boolean b) {
if (b) {
titlebar.setvisibility (view.visible);//Set the title bar to appear on the view ...
} else{
titlebar.setvisibility (view.gone);/Set the title bar does not appear on the view ...
}
1.2 Add a new control to the title bar ...
Whether you want to add a control dynamically in the title bar or in an activity, it's definitely a process of adding a view ... Before adding a view, we need to add the control to the view and then add the view to the activity, which completes the effect of dynamically adding views ... The view is actually just an empty shelf, more image is a not playing the same balloon. Light has a shell, nothing, then if you want to have something, then we need to inflate it, only after the gas-filled view is what we want to load ... Otherwise it's no use.
The process of inflating is to use Layoutinflater.inflater (r.layout.xxx,root) function to complete ... Familiar with the use of ListView then presumably also very clear this process ... When using ListView, we just write the layout of the item in ListView, but never write the items in the layout in the dead item layout. Because things in the item are generally dynamically added ... So we generally get the ID of the ListView and then dynamically load each item ... This is generally the operation of the ListView ... In fact, all the same ... Add views are dynamically added ... After adding the view, we can still do more by getting the control and then binding the listener ...
This function is a function of my custom ... arg0 and arg1 are the views obtained through inflater.inflate ...
public void Setrightview (int resid0,int resid1) {
clearrightview ();
Titlebar.addrightview (arg0); Add a view
to the title bar ... Titlebar.addrightview (arg1);
moreview= (Button) Arg0.findviewbyid (RESID0);//Get control
from current view ... moreapp= (Button) Arg1.findviewbyid (RESID1);
After you get the control, set up related listening to do more ...
Moreview.setonclicklistener (New View.onclicklistener () {
@Override public
void OnClick (View v) {
//TODO auto-generated method Stub
toast.maketext (Context, "click", Toast.length_long). Show ();
}
Moreapp.setonclicklistener (New View.onclicklistener () {
@Override public
void OnClick (View v) {
// TODO auto-generated Method Stub
toast.maketext (Context, "Continue click", Toast.length_long). Show ();}}
This completes the addition of the view ... In fact, the operation of the title bar is basically the process ... The most important thing is to know exactly what the function is, how to set the relevant properties for the title bar, such as background color, title bar text, title bar logo, and some related properties of the settings, in fact, is to put XML that layout way through Java code to more flexible to use ... You can make the controls on the layout more flexible ...
1.3 Action Bar Transform ...
Let's take a look at a function to see what functions Mabtitlebar have to dynamically set view view ... Here is to introduce some of the relevant functions of the application ... There is no complete code process ... The source code process I will finally give the ...
Btn5.setonclicklistener (New View.onclicklistener () {@Override The Call of the public void OnClick (View v) {//The following function does not have a function order problem ... The different call order shows the same result as the,,, Mabtitlebar.setlogo (r.drawable.button_selector_delete);
Set the logo for the title bar ... Mabtitlebar.setlogo2 (R.drawable.button_selector_app); Set a second logo for the title bar ... This setlogo only these two functions. No Logo3 ... If you want to set more logos ... can only write XML layout ... mabtitlebar.clearrightview ();
Clear the view to the right of the title bar ... View Rightviewok = minflater.inflate (r.layout.ok_btn, NULL); Inflate ... mabtitlebar.addrightview (Rightviewok) for a view
Add the currently inflated view to the right of the title bar ... Mabtitlebar.settitletext ("being modified");
Set the Text property of the title bar ...
Mabtitlebar.settitlebarbackground (R.DRAWABLE.TOP_BG2);//Set the background of the text ... Mabtitlebar.settitlebargravity (Gravity.left,gravity.center)//Set the title bar to its way ... mabtitlebar.setlogoonclicklistener ( New Onclicklistener () {///For logo picture Setup listening ... @Override public void OnClick (View v) {//Change related properties ... Mabtitlebar.sett
Itlebarbackground (R.DRAWABLE.TOP_BG);
Mabtitlebar.settitletext ("Multifunctional title bar"); Mabtitlebar.clearrightview ();
Mabtitlebar.setlogo (R.drawable.button_selector_back);
Mabtitlebar.settitlebargravity (Gravity.left,gravity.right);
Mabtitlebar.settitletextmargin (20, 0, 0, 0); Mabtitlebar.getlogoview2 (). setvisibility (View.gone); Set visibility ... mabtitlebar.setlogoonclicklistener (new Onclicklistener () {@Override public void OnClick (View V
) {finish ();
}
});
}
});
}
});
1.4 Drop-down Effect of the title bar ...
The bottom of the title bar is actually clicking the title to form a Drop-down menu. Each Drop-down menu is a sub option ... With these sub options we can do more things ... Effect as above ... In fact, every such similar menu is a ListView. By initializing each item of the ListView, it is also natural to form this effect ... In fact, the overall or dynamic layout ...
Btn7.setonclicklistener (new View.onclicklistener () {@Override public void OnClick (
View v) {mabtitlebar.settitletextbackgroundresource (R.DRAWABLE.DROP_DOWN_TITLE_BTN); View Popview = minflater.inflate (R.layout.list_pop, NULL);
Get a view first ... ListView Poplistview = (ListView) Popview.findviewbyid (r.id.pop_list);
Gets the ID of the ListView ... list<abmenuitem> list = new arraylist<abmenuitem> (); Define a list item ...
This class saves only the ID and Text properties ...
List.add (New Abmenuitem ("Wenji"));
List.add (New Abmenuitem ("Diao cicada"));
List.add (New Abmenuitem ("Purple poppy"));
List.add (New Abmenuitem ("Sun Shangxiang")); Listpopadapter mlistpopadapter = new Listpopadapter (titlebaractivity.this, List,r.layout.list_pop_item); Define an adapter ... poplistview.setadapter (mlistpopadapter); Set an adapter for ListView ... Mabtitlebar.settitletextdropdown (Popview);
Set the TextView on the titlebar to be tapped.
}
});
With this setting, it's easy to create this effect. The Abmenuitem class is used here ... From the source we can know that this class has two constructors, one is to the int ID and string text Two properties to save, one is to save only a string Text property ... This also makes the ListView display of things relatively simple ... And generally this small listview is usually a text value. Therefore, it is more convenient to use abmenuitem ... This also completes a drop down menu ...
1.5 adapter ...
Simply say the adapter this thing ... Adapters are mainly used to dynamically set each item in the ListView, making the layout of the ListView more flexible ... To be blunt is to define a style, each item in ListView need to follow this style to show ... It's very simple ...
* * * copyright@ by darker 2015-10-30 * multi-function Menu ... * * * * * Package com.example.andbasetitlebar;
Import java.util.List;
Import Android.content.Context;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.TextView;
Import Com.ab.global.AbMenuItem;
public class Adapter extends Baseadapter {//Inheritance Baseadapter private context context;
Private list<abmenuitem> List;
private int itemresource;
Public Adapter (context context, list<abmenuitem> List,int itemresource) {this.context = context;
This.list = list;
This.itemresource = Itemresource;
@Override public int GetCount () {return list.size ();
@Override public Object getitem (int position) {return list.get (position);
@Override public long getitemid (int position) {return position; //Because the item in our ListView is displayed in a very simple way, the Viewholder class defines just one TextView property ... @Override public View getview (int poSition, View Convertview, ViewGroup viewgroup) {Viewholder holder; /* This judgment is actually defined for reuse ... Because the ListView are loaded dynamically. * If the item in the ListView exceeds the size of the screen, then there is bound to be a drop down ... * the pull on behalf of the original ListView will need to be destroyed ... * then you can't always slide every time we create a new Viewholder object. We can reuse these destroyed viewholder ... * after the reuse, the Viewholder will automatically invoke those that are destroyed to reuse when it is reused ... So that the resources are reloaded ... * to put it bluntly is to improve the reusability ... * */if (Convertview = null) {Convertview = Layoutinflater.from (context). Inflat E (Itemresource, NULL);
Define a View object ... holder = new Viewholder (); Holder.itemtext = (TextView) Convertview.findviewbyid (R.id.pop_item);
Gets the ID of the ListView in the current view ...
Convertview.settag (holder);//Set flag ...
else {holder = (Viewholder) Convertview.gettag ()///Here you can get the flags directly, reuse the holder, and reduce the allocation and overhead of memory ...}
Abmenuitem item = list.get (position); Holder.itemText.setText (Item.gettext ());
Set the value for the TextView in each item ... return convertview;
Static class Viewholder {TextView itemtext;
}
}
Implementation of the Multi-function menu is very simple, most of the functions are written functions, we only need to call to set the relevant style of the title bar ... Implement a versatile menu ... Finally append the code of my own writing. Or the complete code can make us progress more quickly ... So you can understand the calling process and the principle. 、
SOURCE Download: Android andbase Framework to implement the multi-function title bar
The above is the entire content of this article, I hope to help you learn.