Actionbar compatible with earlier versions (4) add tab + add custom Tab View

Source: Internet
Author: User

It is a useful technique to add a tab in the actionbar. With the support of the support V7 database, we can add tabs in almost the same way as before. For tabs, we can define their own views like menuitem. I have defined a progress bar to illustrate this problem. This series of blog posts will also come to an end, followed by an example of defining the actionbar style and a pattern. By the way, we will explain the usage of fragment and viewpager.

 

Activity_main.xml (defines a fragment)

<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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.kale.actionbar03.MainActivity" >    <FrameLayout        android:id="@+id/container_id"        android:layout_width="match_parent"        android:layout_height="match_parent" /></RelativeLayout>

 

Tab_custom_view.xml (custom Tab View)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent">    <ProgressBar        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:indeterminate="true"/></LinearLayout>

 

Mainactivity. Java

Package COM. kale. actionbar03; import android. OS. bundle; import android. support. v4.app. fragment; import android. support. v4.app. fragmenttransaction; import android. support. v7.app. actionbar; import android. support. v7.app. actionbar. tab; import android. support. v7.app. actionbaractivity; import android. widget. toast; public class mainactivity extends actionbaractivity implements actionbar. tablistener {actionbar; Private Static final string selected_item = "selected_item"; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); actionbar = getsupportactionbar (); // actionbar. setdisplayshowhomeenabled (false); // actionbar. setdisplayshowtitleenabled (false); // after the two rows are defined, there will be no title bar, and only the tab bar will be set with the tab actionbar. setnavigationmode (actionbar. navigation_mode_tabs); actionbar. tab tab; // start to add tab for (INT I = 1; I <= 3; I ++) {tab = actionbar. newtab (); tab. settext ("tab" + I); tab. settablistener (this); actionbar. addtab (Tab);} tab = actionbar. newtab (); tab. setcustomview (R. layout. tab_custom_view); tab. settext ("X"); tab. settablistener (this); actionbar. addtab (Tab) ;}@ override protected void onrestoreinstancestate (bundle savedinstancestate) {super. onrestoreinstancestate (savedinstancestate); If (savedinstancestate. containskey (selected_item) {// select the fragment page actionbar corresponding to the saved index. setselectednavigationitem (savedinstancestate. getint (selected_item) ;}@ override protected void onsaveinstancestate (bundle outstate) {// Save the index of the currently selected fragment page to outstate in the bundle. putint (selected_item, actionbar. getselectednavigationindex (); super. onsaveinstancestate (outstate);}/** @ see android. support. v7.app. actionbar. tablistener # ontabreselected (Android. support. v7.app. actionbar. tab, android. support. v4.app. fragmenttransaction) * method executed when tab is selected again */@ override public void ontabreselected (Tab tab, fragmenttransaction transaction) {}@ override public void ontabselected (Tab tab, fragmenttransaction transaction) {toast. maketext (getapplicationcontext (), Tab. gettext (), 0 ). show (); // create a new fragment object fragment = new dummyfragment (); // create a bundle object, used to pass bundle = new bundle (); bundle to fragment. putint (dummyfragment. bundle_section_number, Tab. getposition () + 1); // input the fragment parameter to fragment. setarguments (bundle); fragmenttransaction Ft = getsupportfragmentmanager (). begintransaction (); // use fragment ft. replace (R. id. container_id, fragment); ft. commit () ;}@ override public void ontabunselected (Tab tab, fragmenttransaction transaction ){}}

 

Dummyfragment. Java

Package COM. kale. actionbar03; import android. OS. bundle; import android. support. annotation. nullable; import android. support. v4.app. fragment; import android. view. gravity; import android. view. layoutinflater; import android. view. view; import android. view. viewgroup; import android. widget. textview; public class dummyfragment extends fragment {public static final string bundle_section_number = "section_number"; // The returned value is the view component displayed by fragment @ override public view oncreateview (layoutinflater Inflater, @ nullable viewgroup container, @ nullable bundle savedinstancestate) {super. oncreateview (Inflater, container, savedinstancestate); textview = new textview (getactivity (); textview. setgravity (gravity. center); // obtain the bundle = getarguments () passed in when this fragment is created; textview. settext ("fragment 0" + bundle. getint (bundle_section_number); textview. settextsize (50); Return textview ;}}

 

Source code download: http://download.csdn.net/detail/shark0017/7688459

 

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.