return(new AnalogClock (DynamicTabDemo.this));

     }

    });

    spec.setIndicator(Clock);

    tabs.addTab(spec);

   }

  });

 }

}

In our button’s setOnClickListener() callback, we create a TabHost.TabSpec object and give it an anonymous TabHost.TabContentFactory. The factory, in turn, returns the View to be used for the tab — in this case, just an AnalogClock. The logic for constructing the tab’s View could be much more elaborate, such as using LayoutInflater to construct a view from layout XML.

In Figure 10-7 you can see that initially, when the activity is launched, we just have the one tab whereas Figure 10-8 shows multiple tabs.

Figure 10-7. The DynamicTab application, with the single initial tab

Figure 10-8. The DynamicTab application, with three dynamically-created tabs

Intents and Views

In the preceding examples, the contents of each tab were set to be a View, such as a Button. This is easy and straight-forward, but it is not the only option. You can also integrate another activity from your application via an Intent.

Intents are ways of specifying something you want accomplished, then telling Android to go find something to accomplish it. Frequently, these are used to cause activities to spawn. For example, whenever you launch an application from the main Android application launcher, the launcher creates an Intent and has Android open up the activity associated with that Intent. This whole concept, and how activities can be placed in tabs, will be described in greater detail in Chapter 25.

Flipping Them Off

Sometimes, you want the overall effect of tabs (only some Views visible at a time), but you do not want the actual UI implementation of tabs. Maybe the tabs take up too much screen space. Maybe you want to switch between perspectives based on a gesture or a device shake. Or maybe you just like being different.

The good news is that the guts of the view-flipping logic from tabs can be found in the ViewFlipper container, which can be used in other ways than the traditional tab.

ViewFlipper inherits from FrameLayout, just like we used to describe the innards of a TabWidget. However, initially, it just shows the first child view. It is up to you to arrange for the views to flip, either manually by user interaction, or automatically via a timer.

For example, here is a layout for a simple activity (Fancy/Flipper1) using a Button and a ViewFlipper:

<?xml version='1.0' encoding='utf-8'?>

<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android'

 android:orientation='vertical'

 android:layout_width='fill_parent'

 android:layout_height='fill_parent'

 >

 <Button android:id='@+id/flip_me'

  android:layout_width='fill_parent'

  android:layout_height='wrap_content'

  android:text='Flip Me!'

 />

 <ViewFlipper android:id='@+id/details'

  android:layout_width='fill_parent'

  android:layout_height='fill_parent'

 >

  <TextView

   android:layout_width='fill_parent'

   android:layout_height='wrap_content'

   android:textStyle='bold'

   android:textColor='#FF00FF00'

   android:text='This is the first panel'

  />

  <TextView

   android:layout_width='fill_parent'

   android:layout_height='wrap_content'

   android:textStyle='bold'

   android:textColor='#FFFF0000'

   android:text='This is the second panel'

  />

  <TextView

   android:layout_width='fill_parent'

   android:layout_height='wrap_content'

   android:textStyle='bold'

   android:textColor='#FFFFFF00'

   android:text='This is the third panel'

  />

 </ViewFlipper>

</LinearLayout>

Notice that the layout defines three child views for the ViewFlipper, each a TextView with a simple message. Of course, you could have very complicated child views, if you so chose.

To manually flip the views, we need to hook into the Button and flip them ourselves when the button is clicked:

public class FlipperDemo extends Activity {

 ViewFlipper flipper;

 @Override

Вы читаете Beginning Android
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату