Adding Them Up
is set up to allow you to easily define tabs at compile time. However, sometimes, you want to add tabs to your activity during runtime. For example, imagine an email client where individual emails get opened in their own tab for easy toggling between messages. In this case, you don’t know how many tabs or what their contents will be until runtime, when the user chooses to open a message.
Fortunately, Android also supports adding tabs dynamically at runtime.
Adding tabs dynamically at runtime works much like the compile‑time tabs previously shown, except you use a different flavor of , one that takes a instance. This is just a callback that will be invoked – you provide an implementation of and use it to build and return the Let’s take a look at an example ().
First, here is some layout XML for an activity that sets up the tabs and defines one tab, containing a single button:
What we want to do is add new tabs whenever the button is clicked. That can be accomplished in just a few lines of code:
Public class extends
Public onCreate
Super onCreate
SetContentView
Final findViewById
Setup
NewTabSpec
SetContent
SetIndicator
AddTab
SetCurrentTab
GetCurrentView findViewById
SetOnClickListener new OnClickListener
Public onClick
Дата добавления: 2015-05-16; просмотров: 631;