| First, the problem description |
Before we used the head navigation in the Viewpager+fragment area, Google added a new actionbar after Android 3.0 to make it easy to implement the design of the head area of the screen such as Return key, title, ICON, Logo, Customizing view and menus, and enabling tab navigation, we use Actionbar to implement the interface design shown below:
| Ii. main components of the case |
1, write Tbactivity
public class Tbactivity extends Activity {private ActionBar ActionBar; protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.LAYOUT.ACTIVITY_TB); Initactionbar (); InitTab (); } @SuppressLint ("Newapi") private void Initactionbar () {Actionbar=super.getactionbar ();//Get ActionBar A Ctionbar.setdisplayshowhomeenabled (TRUE);//Display Home area actionbar.setdisplayhomeasupenabled (TRUE);//display return picture actio Nbar.sethomeasupindicator (R.drawable.back)///Set back icon//Remove the default icon icon drawable colordrawable=new Colordrawable (Android. R.color.transparent); Actionbar.seticon (colordrawable); Set Custom View actionbar.setdisplayshowcustomenabled (TRUE); Actionbar.setcustomview (R.layout.head_logo); Set the navigation mode to TABS way Actionbar.setnavigationmode (actionbar.navigation_mode_tabs); } private void InitTab () {Bundle bundle1=new BundLe (); Bundle1.putstring ("title", "Excavator Block information"); tab tab1= Actionbar.newtab (). SetText ("excavator"). Settablistener (New tablistener<firstfragment> (This, "excavator", Fi Rstfragment.class,bundle1)); Actionbar.addtab (TAB1); Bundle Bundle2=new Bundle (); Bundle2.putstring ("title", "Loader block information"); tab Tab2=actionbar.newtab (). SetText ("loader"). Settablistener (New tablistener<secondfragment> (This, "loaded Carrier ", secondfragment.class,bundle2)); Actionbar.addtab (TAB2); Bundle Bundle3=new Bundle (); Bundle3.putstring ("title", "Grader Block Information"); tab Tab3=actionbar.newtab (). SetText ("Grader"). Settablistener (New tablistener<thirdfragment> (This, "grader"). , Thirdfragment.class,bundle3)); Actionbar.addtab (TAB3); Bundle Bundle4=new Bundle (); Bundle4.putstring ("title", "More block information"); tab Tab4=actionbar.newtab (). SetText ("more"). Settablistener (New TabListEner<fourfragment> (This, "more", fourfragment.class,bundle4)); Actionbar.addtab (TAB4); public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the action bar if it I s present. Getmenuinflater (). Inflate (R.MENU.TB, menu); return true; }}
2, Tablistener component
public class Tablistener<t extends fragment> implements android.app.actionbar.tablistener{private Fragment Mfrag ment; Private final Activity mactivity; Private final String Mtag; Private final class<t> MClass; Private bundle bundle; Public Tablistener (activity activity, String tag, class<t> clz,bundle Bundle) {mactivity = activity; Mtag = tag; MClass = CLZ; This.bundle=bundle; } @Override public void ontabselected (Tab tab, fragmenttransaction ft) {//TODO auto-generated method stub if (mfragment==null) {mfragment=fragment.instantiate (mactivity, Mclass.getname ()); Mfragment.setarguments (bundle);//pass parameter Ft.add (Android) to fragment. R.id.content,mfragment, Mtag); }else{Ft.attach (mfragment); }} @Override public void ontabunselected (Tab tab, fragmenttransaction ft) {if (mfragment!=null) { Ft.detach (mfragment); } } @Override public void ontabreselected (Tab tab, fragmenttransaction ft) {}}
3, Design four tab area of the fragment
respectively: (Firstfragment, Secondfragment, Thirdfragment, fourfragment), here firstfragment as an example (other here no difference)
public class Firstfragment extends Fragment {private String title; @Override public void Setarguments (bundle bundle) {//TODO auto-generated method stub title=bundle.getst Ring ("title"); } @Override Public View oncreateview (layoutinflater inflater, ViewGroup container, Bundle Savedin Stancestate) {TextView TextView = new TextView (getactivity ()); Textview.settext (title); Textview.settextsize (20); Textview.settextcolor (color.red); Textview.setgravity (Gravity.center); LinearLayout layout = new LinearLayout (getactivity ()); Layoutparams params = new Layoutparams (layoutparams.match_parent, layoutparams.match_parent); Layout.addview (TextView, params); return layout; }}
4, Defines the style of the Actionbar
<style name= "Myactionbar" parent= "@android: Style/theme.holo.light" > <item name= "android:actionbarstyl E "> @style/actionbar_style</item> <!--define style resources for the Options tab in the Action Bar--<item name=" android:actionb Artabstyle "> @style/myactionbartabstyle</item> <!--define style resources for the text in the Navigation Options tab. --<item name= "Android:actionbartabtextstyle" > @style/myactionbartabtextstyle</item> </ Style> <!--actionbar Style--<style name= "Actionbar_style" parent= "@android: Style/widget.holo.ligh T.actionbar "> <item name=" android:background "> #EA4609 </item> <item name=" Android:titletextstyl E "> @style/actionbar_titlestyle</item> <item name=" Android:icon "> @android: color/transparent</ item> <item name= "android:itempadding" >20dip</item> </style><!--tab Options tab Style--<s Tyle name= "Myactionbartabstyle" parent= "@android: Style/widget.hoLo. Light.ActionBar.TabView "> <item name=" android:background "> @drawable/tab_selector</item> <item Name= "Android:paddingleft" >0dp</item> <item name= "Android:paddingright" >0dp</item> <i TEM name= "Android:height" >100dp</item> </style> <!--tab option label font style--<style name= "Myac Tionbartabtextstyle "parent=" @android: Style/widget.holo.light.actionbar.tabtext "> <item nam E= "Android:textcolor" > #666666 </item> <item name= "android:textsize" >18sp</item> </styl E>
Set style Android:theme= "@style on tbactivity/myactionbar
To learn more about the small partners, you can click to view the source code , run the test yourself.
Inquiries or technical exchanges, please join the official QQ Group: (452379712)
Jerry Education
Source:http://blog.csdn.net/jerehedu/
This article is the copyright of Yantai Jerry Education Technology Co., Ltd. and CSDN Common, welcome reprint, but without the author's consent must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Typical interface design (4)--Use Actionbar+fragment for tab switching