Same as the
previous example,
but each tab now invokes a
Fragment
instead of an
Activity
.
The deprecated class
TabActivity
is no longer used.
The tabs appear in the
ActionBar
.
See
Fragments
and
Action Bar.
At any time,
there has to be a selected tab.
When the first tab is created,
it is therefore selected.
The
addTab
method selects the third tab,
unselecting the first one.
When the app is launched,
we see the following
Toast
messages:
“reselected” means you pressed the same tab twice in a row.
Each
Fragment
has an
onCreateView
method.
MyTabActivity.java
Tab1Fragment.java
Tab2Fragment.java
Tab3Fragment.java
main.xml
:
a
TabHost
containing a
TabWidget
and a
FrameLayout
.
The
id
numbers
android.R.id.tabs
)android.R.id.tabcontent
)ic_tab2.xml
contains a
State
List:
selected and unselected.
ic_tab2_grey.png
:
selected, 32 × 32 pixels.
ic_tab2_white.png
:
unselected.
AndroidManifest.xml
has four
activity
elements.
Same as the above example.
Press the “Toggle tab mode” button first.
The
ActionBar
holds tabs.
Each tab holds a
TabListener
.
Each
TabListener
holds a
Fragment
.
Each
Fragment
holds a
View
.
Retains fragment state of the non-visible tabs across
Activity
instances.
ActionBar
looks ugly:
all uppercase,
no space between icon and text.
Can you clean it up with
setCustomView
?